Jump to content

passing serialized object in URL not working


Go to solution Solved by mac_gyver,

Recommended Posts

Hi all,
 
I have been trying to figure out by myself for hours but with no success.
 
I created a object and then serialized it and passed in a URL.

include_once("BabyName.php");
                        $babyName = new BabyName($name, $gender, $meaning, $origin, $likesCount);
                        echo $babyName->getGender();
                        $babyNameSerialized = serialize($babyName);
                        echo $babyNameSerialized;

<a href="babyNameDetails.php?id= '<?php echo $babyNameSerialized ?>'"> <?php echo $name ?></a>

I get output:O:8:"BabyName":5:{s:18:"BabyNamebabyName";s:6:"Akriti";s:16:"BabyNamegender";s:4:"Girl";s:17:"BabyNamemeaning";s:16:"Picture, Diagram";s:16:"BabyNameorigin";N;s:19:"BabyNamenoOfLikes";s:1:"1";} 

 

Then in babyNameDetails.php file, I get the seriazed babyName. But only O:8 gets printed. It gets serialized properly originally as I can see the echo output. But when I pass through URL, I do not get the serialized string.

<?php include_once('BabyName.php');
                        $id = $_REQUEST['id'];
                        echo "id: " . $id . "<br>";
                        $babyName = unserialize($id);
                        echo "babyName: " . $babyName;

I just get:

id: 'O:8:
babyName:

 

Please let me know what am I doing wrong.

 

Thank you,

Roshni

 

 

try making a url variable and echo it back:

 

 

$url = "babyNameDetails.php?".$babyNameSerialized;
echo $url;
echo "<a href=\"{$url}\">{$name}</a>";

Then on the receiving page stick in

print_r($_GET)
and see what's sent over

 

 

Also:  ONLY ONE THREAD IS REQUIRED, POSTING MULTIPLE DUPLICATES IS AGAINST FORUM T's&C's (not to mention it upsets people...like me....and discourages those people from helping you)

i am so sorry I posted more than once. I did not realized I had posted more than once because everytime I hit the submit post button I would get "page cannot be displayed" page. I thought I was not able to post my problem on this forum at all. Thanks for the response. 

i am so sorry I posted more than once. I did not realized I had posted more than once because everytime I hit the submit post button I would get "page cannot be displayed" page. I thought I was not able to post my problem on this forum at all. Thanks for the response. 

 

If it makes you feel any better, you're not the only one who's had these problems. I (along with the other gurus, moderators, and admins) do our best to remove the duplicate responses in a timely fashion. Note that your duplicates have been removed.  :happy-04:

  • Like 1
  • Solution

the serialized data contains several characters that have significance in a url and in html as delimiters/syntax and must be urlencoded() so that they can exist as part of the value in the url.

 

php will url decode the $_GET values for you.

Thank you CyberRobot.

 

I tried serializing then encoding ...then sending over URL then decoding and then unserializing. But still I am having problems.

$name = "Akriti";
$babyNameSerialized = urlencode(serialize($babyName));
$url = "babyNameDetails.php?".$babyNameSerialized;
<td style="font-weight: bold;" ><?php echo "<a href=\"{$url}\">{$name}</a>"; ?></td>

and in 

<?php include_once('BabyName.php');
                        print_r ($_GET);
                        $id = $_REQUEST['id'];
                        echo "<br>" . "id: " . $id . "<br>";
                        $babyName = unserialize(urldecode($_GET['id']));
                        echo "babyName: " . $babyName;

This is what I get:

 

Array ( [O:8:"BabyName":5:{s:18:"] => )

id: 
babyName:

 

I can see the encoded data in the URL though. So the BabyName object is sent in the URL but I am not able to receive it.

 

But I am not able to get in the $_GET or$_REQUEST. Please suggest me.

 

Thanks,

Roshni

Edited by roshni

Thank you all for the valuable suggestions for my problem. It helped solve it.

 

I now am having issue with the lost method of the object. I get the following output:

 

Array ( [id] => O:8:"BabyName":5:{s:18:"BabyNamebabyName";s:6:"Akriti";s:16:"BabyNamegender";s:4:"Girl";s:17:"BabyNamemeaning";s:16:"Picture, Diagram";s:16:"BabyNameorigin";N;s:19:"BabyNamenoOfLikes";s:1:"1";} )object(BabyName)[7]
private 'babyName' => string 'Akriti' (length=6)
private 'gender' => string 'Girl' (length=4)
private 'meaning' => string 'Picture, Diagram' (length=16)
private 'origin' => null
private 'noOfLikes' => string '1' (length=1)
 

( ! ) Fatal error: Call to undefined method BabyName::getName() in C:\wamp\www\babyNames\babyNameDetails.php on line 34 Call Stack # Time Memory Function Location 1 0.0011 688280 {main}( ) ..\babyNameDetails.php:0

 

I have this on my php page:

<?php include_once('BabyName.php');
                        print_r ($_GET);
                        $babyName = unserialize(urldecode($_GET['id']));
                        var_dump($babyName);
                        echo "babyName: " . $babyName->getName();

It seems we cannot serialize object methods. How do we work around getting the object methods? Or is their another way of doing it?

 

Thank you,

Roshni

Edited by roshni
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.