Page 1 of 1

passing array using GET_

Posted: Mon Oct 12, 2009 6:18 am
by giorgioca
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:

Code: Select all

 
$myarray = (1,2);
 
Any suggestion on why I'm not able to pass strings?
Thanks!
G

Re: passing array using GET_

Posted: Mon Oct 12, 2009 7:11 am
by turbolemon
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>

Re: passing array using GET_

Posted: Mon Oct 12, 2009 7:36 am
by giorgioca
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:a:2:{i:0;s:5:\"item1\";i:1;s:5:\"item2\";}
unserialized:

basically, i can't "unserialize" the string!

Thanks,
G

Re: passing array using GET_

Posted: Mon Oct 12, 2009 7:39 am
by jackpf
urldecode() ;)

Re: passing array using GET_

Posted: Mon Oct 12, 2009 7:50 am
by giorgioca
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:

Re: passing array using GET_

Posted: Mon Oct 12, 2009 8:25 am
by jackpf
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.

Re: passing array using GET_

Posted: Mon Oct 12, 2009 9:06 am
by giorgioca
Thanks jackpf,

I had Magic Quotes On in the php.ini... problem solved!

G

Re: passing array using GET_

Posted: Mon Oct 12, 2009 9:14 am
by jackpf
Cool cool


:)