Passing Arrays through URL and $_Get[]

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

Post Reply
tyeh26
Forum Newbie
Posts: 3
Joined: Thu Jun 19, 2008 12:04 am

Passing Arrays through URL and $_Get[]

Post by tyeh26 »

hey all,
I'm a pretty new PHP programmer. Used to more object oriented java/C++, learned PHP on my own (as opposed in a class), and i'm working on this project. Here's what I want to do. Create an array in 1 page. And in page 2 be able to recall that same array. This array might be big, filled with Strings and other Arrays. So i thought maybe using serialize() might work. Here's my test code:

Code: Select all

<?php
    $refine = $_GET["refine"];
    if ($refine == NULL) {
        $refine = array("one","two","three","four");
        $ser_refine = serialize($refine));
        echo "<a href=\"test.php?refine=".$ser_refine."\">Click Me</a>";
    }
    else {
        $ser_refine = unserialize($refine));
        var_dump($ser_refine);
    }
?>
 
So what this does is Gets the variable in the URL called refine. If refine wasn't there, then it creates and array, and a link with it serialized inside it. Once you click on it, when it Gets it again, it will be defined. So it tries to unserialize it and var_dumps it.

Well, it isn't working. the unserialize must be failing because $ser_refine is a FALSE boolean.

Am i doing this completely wrong? I've seen elsewhere that you can just split each item by a comma into a long string then recreating the array. This seems like a very low-tech way to do it. Anyways, any help will.... help. Thanks all in advance.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Passing Arrays through URL and $_Get[]

Post by jayshields »

This triggered an old problem of mine which I actually ended up giving up on, but after about 15 minutes of seeing yours, I had to solve it.

Code: Select all

<?php
    if(!isset($_GET['ser']))
        echo '<a href="serialize.php?ser='.base64_encode(serialize(array('one', 'two', 'three'))).'">Link</a>';
    else
        var_dump(unserialize($_GET['ser']));
?>
tyeh26
Forum Newbie
Posts: 3
Joined: Thu Jun 19, 2008 12:04 am

Re: Passing Arrays through URL and $_Get[]

Post by tyeh26 »

hey, thanks for the reply. I haven't been able to test the code yet because I am not at home, But just making sure you don't need a base64_decode(). While i don't understand what it does, if you encode it, don't you have to decode it?

edit:
after looking at:
http://www.php.net/serialize
It seems that serialize() has some special characters not suitable for a URL?
and base64_encode() gets rid of them? thanks again for you help guys.
tyeh26
Forum Newbie
Posts: 3
Joined: Thu Jun 19, 2008 12:04 am

Re: Passing Arrays through URL and $_Get[]

Post by tyeh26 »

Thank you, this works ( if you add the decode line)

thank you very much again.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Passing Arrays through URL and $_Get[]

Post by jayshields »

Sorry, some how it worked for me while I was messing around with it, and when I grabbed the script it didn't include base64_decode()... weird.

Anyway, for future reference, the working version is

Code: Select all

<?php
    if(!isset($_GET['ser']))
        echo '<a href="serialize.php?ser='.base64_encode(serialize(array('one', 'two', 'three'))).'">Link</a>';
    else
        var_dump(unserialize(base64_decode($_GET['ser'])));
?>
Post Reply