Jump to content

Serialize / unserialize problem


vineld

Recommended Posts

I have run into problems using serialize / unserialize together with urlencode. In the first file I do this:

 

$a = urlencode(serialize($aArray));
echo count(unserialize(urldecode($a)));

 

This results in the correct number of items in the array.

 

I then send $a to another file where this is done:

 

$a = $_GET["a"];
$a = urldecode($a);
echo $a;

$aArray = unserialize($a);
echo count($aArray);

 

This simply prints 1 and the array has not been restored. What is the problem here? I have simplified the code somewhat, I actually return js for an ajax request in the second file.

 

I have alerted the $a string in the second file, copied it and ran unserialize in another simple php file and then it works...

Link to comment
Share on other sites

Based on the code snippets that you have written, it seems that in order to solve this problem for you, we are going to need to see more. I dont see anything that sticks out as wrong here so the error has to be elsewhere. Basically we just need to see what is going on around these lines because I suspect that:

1. these lines are not getting executed or

2. the transfer between the two files is not supplying the parameters that you expect

 

chris.

Link to comment
Share on other sites

Try not doing the urldecode.  I believe that once the data comes across the $_GET, it is automatically decoded.  For example, if you pass "hello%20world" as a get parameter and then echo it on the page, it will echo as "hello world" and not "hello%20world".  Your urldecode may be destroying some stuff by decoding things that shouldn't be decoded.  I could see a definate issue if for instance you have the "%" char anywhere in your serialized array.  Also, you probably don't want to be passing entire arrays of stuff via GET unless you have no other alternative.  I would stick to session variables for that sort of thing...

Link to comment
Share on other sites

There isn't really any more code of relevance to show. It's a simple ajax request.

 

Yeah, I am usually using sessions but I wanted to experiment with this alternative in case there is a situation where it would be needed (can't really think of one now though...). It seems as if the $_GET is getting in the way so to speak. I have tried removing the urldecode as well although it makes no difference. I suspect it has to do with the character encoding though.

 

Using sessions it works just fine.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.