As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/
Options

PHP cookie question.

Spectral SwallowSpectral Swallow Registered User regular
So for my website I'm trying to store an array in a cookie and call it back whenever. After searching through the first 10 pages of google I'm at a complete loss. Below is a super simplified version of the code.

On the first page I have:
$Info = array( 'Bob', '21');
setcookie("user", $info, time()+3600);


And then on the following page I have the code:
$WOO= $_COOKIE["user"];
echo $WOO[0];

I've tried serializing and unserializing to no avail. It has to be something super simple but I just can not figure it out. Any help is greatly appreciated.

Posts

  • Options
    HeartlashHeartlash Registered User regular
    Is your initial code above the doctype? setCookie has to be used before any of the html executes.

    My indie mobile gaming studio: Elder Aeons
    Our first game is now available for free on Google Play: Frontier: Isle of the Seven Gods
  • Options
    Spectral SwallowSpectral Swallow Registered User regular
    No that's all within the <?php ?> before the html.

  • Options
    HeartlashHeartlash Registered User regular
    Try print_r($_COOKIE); (this will show all cookies with their values, etc) and see if user shows up at all. If it does, check what it thinks the value is. This may help you debug. Unfortunately I've never had to set a cookie with an array as the value so I'm not sure if I can help beyond that :(. Good luck.

    My indie mobile gaming studio: Elder Aeons
    Our first game is now available for free on Google Play: Frontier: Isle of the Seven Gods
  • Options
    EndEnd Registered User regular
    edited May 2013
    have you checked your error log?

    Your minimal example without serialization should have given you some sort of error (or at least, it gives me an error). Whatever is preventing it from working even when you serialize it might show up there too.


    edit: additionally, the default path might be too specific. You may need to set the path parameter to setcookie().

    It may help to check your browser's development tools to determine what cookies have been saved.

    End on
    I wish that someway, somehow, that I could save every one of us
    zaleiria-by-lexxy-sig.jpg
  • Options
    Spectral SwallowSpectral Swallow Registered User regular
    Well after checking the suggestions here, I found out that it IS in fact storing the cookie as an array with serialize... I just can't access the array. Updated code:

    First Page (works fine, stores cookie):
    <?php
    $Info = array( 'Bob', '21');
    setcookie("user",serialize($info), time()+3600);
    ?>

    Second page (can't access cookie):
    <?php
    $WOO = unserialize($_COOKIE["user"]);
    echo $WOO[0];

    print_r($_COOKIE);
    //This confirms that the cookie is indeed set.

    ?>

  • Options
    IcyLiquidIcyLiquid Two Steaks Montreal, QuebecAdministrator, Vanilla Staff vanilla
    1) Both pages on the same domain, and at the same path depth?
    2) Could you provide the output of var_dump($_COOKIE) in your second script?

  • Options
    wmelonwmelon Registered User regular
    I would change this line
    setcookie("user",serialize($info), time()+3600);
    
    to this
    setcookie("user",serialize($info), time()+3600, "/");
    

    That will make your cookie available to any page inside your domain.

Sign In or Register to comment.