PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
giorgioca
Forum Newbie
Posts: 4 Joined: Mon Oct 12, 2009 5:59 am
Post
by giorgioca » Mon Oct 12, 2009 6:18 am
Hi,
I'm trying to pass an array of strings from one page to another using the GET method
using the code below:
Code: Select all
<?php
$myarray = ("item1","item2");
$myarray = serialize($myarray);
?>
<a href = "display.php?"values="<?php echo $myarray; ?> ">test</a>
In display.php I'm trying to fetch the array using:
Code: Select all
<?php
$myarray = unserialize($_GET['values']);
print_r ($myarray);
?>
The code somehow works flawless only when the elements of the array are numbers i.e:
Any suggestion on why I'm not able to pass strings?
Thanks!
G
turbolemon
Forum Commoner
Posts: 70 Joined: Tue Jul 14, 2009 6:45 am
Location: Preston, UK
Post
by turbolemon » Mon Oct 12, 2009 7:11 am
I thought the problem was strange.. turns out it's not. Serialize encloses strings in double quotes. If you enclose your entities in HTML with double quotes, it will break the link:
Code: Select all
<a href = "test.php?values=a:2:{i:0;s:2:"ha";i:1;s:2:"he";}">test</a>
Easy fix:
Code: Select all
$myarray = urlencode(serialize($myarray));
Code: Select all
<a href = "test.php?values=a%3A2%3A%7Bi%3A0%3Bs%3A2%3A%22ha%22%3Bi%3A1%3Bs%3A2%3A%22he%22%3B%7D">test</a>
giorgioca
Forum Newbie
Posts: 4 Joined: Mon Oct 12, 2009 5:59 am
Post
by giorgioca » Mon Oct 12, 2009 7:36 am
hi turbolemon,
Thanks! It works just fine.
I still have an issue when fetching the array though.
Code: Select all
$info = $_GET['values'];
echo "serialized:".$info;
$info = unserialize($info);
echo "unserialized:".$info;
echo $info;
print_r ($info);
im getting the following result:
serialized
2:{i:0;s:5:\"item1\";i:1;s:5:\"item2\";}
unserialized:
basically, i can't "unserialize" the string!
Thanks,
G
jackpf
DevNet Resident
Posts: 2119 Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK
Post
by jackpf » Mon Oct 12, 2009 7:39 am
urldecode()
giorgioca
Forum Newbie
Posts: 4 Joined: Mon Oct 12, 2009 5:59 am
Post
by giorgioca » Mon Oct 12, 2009 7:50 am
no luck yet!
Code: Select all
$info = $_GET['values'];
echo "encoded : ".$info."<br>";
$info = urldecode($info);
echo "decoded : ".$info."<br>";
$info = unserialize($info);
echo "unserialzed:".$info;
output:
encoded : a:2:{i:0;s:5:\"item1\";i:1;s:5:\"item2\";}
decoded : a:2:{i:0;s:5:\"item1\";i:1;s:5:\"item2\";}
unserialzed:
jackpf
DevNet Resident
Posts: 2119 Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK
Post
by jackpf » Mon Oct 12, 2009 8:25 am
Oh, you have magic quotes turned on? Either that, or you're running addslashes/some escape function on the data.
Try running it through stripslashes(), or check if magic quotes is turned on.
giorgioca
Forum Newbie
Posts: 4 Joined: Mon Oct 12, 2009 5:59 am
Post
by giorgioca » Mon Oct 12, 2009 9:06 am
Thanks jackpf,
I had Magic Quotes On in the php.ini... problem solved!
G
jackpf
DevNet Resident
Posts: 2119 Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK
Post
by jackpf » Mon Oct 12, 2009 9:14 am
Cool cool