LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-07-2006, 10:33 AM   #1
climbingmerlin
Member
 
Registered: Mar 2004
Location: UK
Distribution: Fedora, OpenSuse
Posts: 91

Rep: Reputation: 15
Javascript array to PHP using cookies problem


Hi,

I am having a little nightmare with a small application that I am writing in PHP. One part of the site allows the user to click on check boxes to select users from a list. This includes a 'select all' option to check all the check boxes. Now this list can be any size in length, so I do not know the values of each box.

When the user submits the selection a javascript function is called that adds all the selected check boxes into an array and should be stored in a cookie. The next page should read this serialized array from the cookie to allow updates to a MySQL database.

The problem that I am having at the moment is that I am getting the following error.

Quote:
Notice: unserialize() [function.unserialize]: Error at offset 2441 of 2447 bytes in /opt/working/mailadmin/include/build_funcs.php on line 226
It appears to me that the cookie is not being sent, but looking at examples and google'ing like a mad man, I can not see why. Below is the javascript that is used for the above functionality:

Code:
var mailchk = null;

function chkAll(){
  mailchk = new Array(document.formbuild.mailshot.length);
  /*Check to see if the checkbox is ticked or not*/
  if(document.formbuild.selectall.checked == true){
    for(var i = 0; i <= document.formbuild.mailshot.length; i++){
      if(document.formbuild.mailshot[i].disabled != true){
        document.formbuild.mailshot[i].checked = true;
        mailchk[i] = document.formbuild.mailshot[i].value;
      }
      
    }
  }else{
    for(var i = 0; i <= document.formbuild.mailshot.length;i++){
      
      document.formbuild.mailshot[i].checked = false;
      mailchk[i] = null;
    }
  }
  
}

function setCookie(name,value,expire){
  document.cookie = name + "=" + escape(value) + ((expire==null) ? "" : (";expires=" + expire.toGMTString()));
}


function js_array_to_php_array(a){
  var a_php="";
  var total = 0;
  for(var key in a){
    ++total;
    a_php = a_php + "s:" +
        String(key).length + ":\"" + String(key) + "\";s:" +
        String(a[key]).length + ":\"" + String(a[key])+ "\";";
  }
  
  a_php = "a:" + total + ":{"+a_php+"}";
  
  return a_php;
  
}

function get_vals(){
  
  if(mailchk == null ){
    add_array();
  }

  var array = js_array_to_php_array(mailchk);
  setCookie("php_array",String(array),null);
  

}


function add_array(){
  mailchk = new Array(document.formbuild.mailshot.length);
  
  for(var i = 0; i <= document.formbuild.mailshot.length;i++){
    if(document.formbuild.mailshot[i].checked == true){
      mailchk[i] = document.formbuild.mailshot[i].value;
    }
  }
}
Here is the PHP code that should read the serialised array from the cookie

Code:
 /*get javascript array from cookie.*/
  $chk_array = unserialize(urldecode(stripslashes($_COOKIE['php_array'])));
Any help on this would be really appreciated.

Thanks...
 
Old 03-08-2006, 03:02 AM   #2
vharishankar
Senior Member
 
Registered: Dec 2003
Posts: 3,142
Blog Entries: 4

Rep: Reputation: 121Reputation: 121
Quote:
When the user submits the selection a javascript function is called that adds all the selected check boxes into an array and should be stored in a cookie. The next page should read this serialized array from the cookie to allow updates to a MySQL database.
Not sure why you need to do this. Can you not simply read the form fields when they are submitted. I'm sure even with dynamically generated forms, it's possible to read the value from the check boxes provided you use PHP to generate their "value" as ids.

What I do is use an array to set the "name" attribute of the HTML checkbox element and then when the form is submitted, you just need to retrieve these.

For example: let's store each checkbox as "id1", "id2" etc. etc. using an array. All you need to do to populate the checkbox is run this through an array to get 1,2,3... etc.

Last edited by vharishankar; 03-08-2006 at 03:05 AM.
 
Old 03-08-2006, 05:22 AM   #3
climbingmerlin
Member
 
Registered: Mar 2004
Location: UK
Distribution: Fedora, OpenSuse
Posts: 91

Original Poster
Rep: Reputation: 15
Sadly not as the values of the check boxes are IDs in a database table, and depending on the search results might not be in a linear format.

That is why I am looking at storing the checkbox values in an array, which is in essence the user id's in the database.

I am interested in a better way of doing this, than using cookies as it does not seem to be the most reliable way of passing the data around the application.

Thanks again in advance.
 
Old 03-08-2006, 06:44 AM   #4
vharishankar
Senior Member
 
Registered: Dec 2003
Posts: 3,142
Blog Entries: 4

Rep: Reputation: 121Reputation: 121
Just retrieve the Ids from the database, use that in an array to set the "name" attribute of the checkboxes in the form field and when you retrieve form, you can access the elements using that ID.

If you set the id to each corresponding check item, it will work. No need for it to be in any order (asc or desc).
 
Old 03-08-2006, 06:52 AM   #5
Spudley
Member
 
Registered: Mar 2003
Location: Berkshire, England.
Distribution: SuSE 10.0
Posts: 299

Rep: Reputation: 32
You're right; cookies aren't that reliable -- users can easily turn them off or delete them. Users can also look at the values of their cookies, and they're transmitted as plain text, so they're not particularly secure, either.

Personally, I'd use PHP sessions rather than keeping everything in cookies. PHP Sessions also use cookies internally when they're available, but can work without them if needed. The great thing is it's all transparent; all you have to to is call session_start() at the beginning of your code on every page, and you get an array called $_SESSION that is persistent for a given user and reasonably secure.

Even better, you don't need to bother with that messy array serialising; just dump all your array values into $_SESSION[], and you've got them to hand whenever you need them.

Hope that helps
 
Old 03-08-2006, 07:06 AM   #6
vharishankar
Senior Member
 
Registered: Dec 2003
Posts: 3,142
Blog Entries: 4

Rep: Reputation: 121Reputation: 121
General advice: even if you use sessions, you should try to encrypt the data. Sessions are usually stored in cookies ...
 
Old 03-08-2006, 07:30 AM   #7
Spudley
Member
 
Registered: Mar 2003
Location: Berkshire, England.
Distribution: SuSE 10.0
Posts: 299

Rep: Reputation: 32
Quote:
Originally Posted by Harishankar
General advice: even if you use sessions, you should try to encrypt the data. Sessions are usually stored in cookies ...
No.... the data in the session array stays safely on the server: the only thing in the cookies is the session ID, and you can't encrypt that.

The only real potential security issue comes from someone trying to take over another user's session by using their session ID, but PHP uses a few other tricks internally (eg IP address) to make sure the session ID being used is legitimate, so it's not easy to hack.
 
Old 03-08-2006, 07:34 AM   #8
vharishankar
Senior Member
 
Registered: Dec 2003
Posts: 3,142
Blog Entries: 4

Rep: Reputation: 121Reputation: 121
Quote:
No.... the data in the session array stays safely on the server: the only thing in the cookies is the session ID, and you can't encrypt that.
That's true, but even then, if somebody could get the session ID (since it's transmitted unencrypted, they could still get the data. Besides if cookies aren't enabled, session Ids are transmitted using URLs and that renders it vulnerable to security problems. The point is that just as you cannot trust cookies, you shouldn't really trusts sessions either. I suggest using a combination of both and/or encrypting data across transfers if you're security conscious.

Besides, I still think that the original question can be solved without discussing either of these issues. The solution I mentioned in my previous post should help.

Last edited by vharishankar; 03-08-2006 at 07:37 AM.
 
Old 03-09-2006, 06:44 AM   #9
climbingmerlin
Member
 
Registered: Mar 2004
Location: UK
Distribution: Fedora, OpenSuse
Posts: 91

Original Poster
Rep: Reputation: 15
All working now, following your guidance... plus a few minor changes to the code.

Many thanks for your help it is very much appreciated.
 
Old 03-09-2006, 08:28 AM   #10
vharishankar
Senior Member
 
Registered: Dec 2003
Posts: 3,142
Blog Entries: 4

Rep: Reputation: 121Reputation: 121
Quote:
Many thanks for your help it is very much appreciated.
Thanks. Exactly which advice did you follow? It might be useful to know just for future reference?
 
Old 03-09-2006, 10:06 AM   #11
climbingmerlin
Member
 
Registered: Mar 2004
Location: UK
Distribution: Fedora, OpenSuse
Posts: 91

Original Poster
Rep: Reputation: 15
I used the method described in post number 4.

Quote:
Just retrieve the Ids from the database, use that in an array to set the "name" attribute of the checkboxes in the form field and when you retrieve form, you can access the elements using that ID.
I was just making things difficult for myself.

Thanks again...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
telephone number array in javascript esteeven Programming 2 06-08-2005 04:02 PM
HTML frames javascript php problem rblampain Programming 4 04-08-2005 02:51 AM
returning an array from a function.. javascript sonesay Programming 1 06-07-2004 05:28 AM
PHP: Multidimensional array problem lhoff Programming 1 06-10-2002 02:49 PM
PHP to mySQL array problem mounters Programming 3 02-17-2002 04:20 PM


All times are GMT -5. The time now is 05:57 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration