PHP - How to pass arrays between pages?

Discussion in 'Web Design and Development' started by mmmdreg, May 7, 2007.

  1. mmmdreg macrumors 65816

    mmmdreg

    Joined:
    Apr 14, 2002
    Location:
    Sydney, Australia
    #1
    Is it possible to do this with cookies at all? I've tried but haven't had much luck..

    G
     
  2. stndn macrumors member

    Joined:
    Oct 22, 2006
    Location:
    earth
    #2
    Do you have to use cookies? Or is session an option for you?

    Care to let us know what you've tried so far?
    Maybe we can point out where you made your mistakes (if any)


    -stndn.
     
  3. ppc_michael Guest

    ppc_michael

    Joined:
    Apr 26, 2005
    Location:
    Los Angeles, CA
    #3
    I think using sessions would most certainly be your best choice. :)
     
  4. oaze macrumors newbie

    Joined:
    May 7, 2007
    #4
    passing an array

    You have several possibilities, which all has advantages and disadvantages.

    • save in cookie
    • save in session
    • send in a form
    • send as parameter in a link

    You have to think first about the sensitivity of the information you pass to next page, is it important that no one can tamper with the information ? .. if so, you can rule out cookie, form or link.

    Do you only need info once or through out the whole user session ?
    usually Sessions and cookies are used for persistant information, and forms and d parameters sent via link are used for inforrmation that are just pased from one page to another.

    Anyway, a convenient method for passing the array is first turning array into a string, using a delimiter to separate each of the components of the array.:

    $comma_separated = implode(",", $array);

    Then on the next page you turn it into an array again:

    $array_again = explode(",", $comma_separated);

    The string can either be sent in a hidden filed in a form, or as parameter on a link. The last you might need to URLencode it.

    also if security is an issue, you could add a checksum of a kind to array passed, and check for consistancy on the next page.

    hope that this helps you...

    kind regards
    /sonny
     
  5. stndn macrumors member

    Joined:
    Oct 22, 2006
    Location:
    earth
    #5
    Actually, instead of combining the values into string, you may want to look into serialize and unserialize.
    Using a delimiter will require you to find out a character that is not used anywhere in your array values to avoid separating the values incorrectly.

    Just be careful of the extra backslashes that may be added to the serialized string, which all depends on your server's PHP settings.

    oaze is right, though.
    The first question to ask is: How important is it for you to keep the information safe and secured?


    -stndn.
     

Share This Page