Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP Cookie question

Options
  • 12-10-2008 5:33pm
    #1
    Closed Accounts Posts: 2,142 ✭✭✭


    I'm trying to write a simple PHP page which will create a cookie which contains the user name of whoever is logged on locally, and then read the username back further on down the PHP page. The code below is the very first thing I've written on the page which is index.php. However this code is not generating a cookie, and the browsers security setting are not blocking cookies either.
    <?php
    setcookie("mycookie", "$username", time() + 3600, "", "mysite.com");
    ?>

    Is there a problem in the syntax or usage of the setcookie command above? I simply cannot get it to work. The webserver this is being run on is Linux.

    The next issue is reading the cookie back once it is created. I was hoping that the following would readback the username into the variable cookieval once it was successfully created from the code above:
    $cookieval = $_COOKIE

    Will this work if the cookie is created?


Comments

  • Registered Users Posts: 3,568 ✭✭✭ethernet


    If I recall correctly, the cookie isn't of any use until you navigate to another page. Try placing some code on another page with the cookie-setting page linking to it.


  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    shamwari wrote: »
    I'm trying to write a simple PHP page which will create a cookie which contains the user name of whoever is logged on locally, and then read the username back further on down the PHP page.
    I don't think that you have access to the user's login name. That would be a security issue.


  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    shamwari wrote: »
    and then read the username back further on down the PHP page.
    Regardless of what value you are setting, your setcookie/readcookie plan will not work.

    In the setcookie docs page, the "Common Pitfalls" section opens with:
    Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);.
    The following code works - it displays the cookie value when the page is reloaded. [php]<?php
    // 24 hour cookie
    setcookie( 'TestCookieName', 'TheCookieValue', time()+60*60*24 );

    echo '<pre>'; print_r( $_COOKIE ); echo '</pre>';

    // Display the 'TestCookieName'. It will not be set until the page is reloaded.
    if ( isset( $_COOKIE ) )
    echo '<p>TestCookieName = ', $_COOKIE, "</p>\n";
    else
    echo '<p>TestCookieName is not set.</p>';
    ?>[/php]


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Also make sure that you've nothing outputted to the browser before that even a single line space.


  • Registered Users Posts: 8,070 ✭✭✭Placebo


    register user on to db with username/pass and assign a reg id along with it.


    on log in get the regid, and store that in a cookie.

    When user comes back, read the regid from cookie and request db for username with that regid.


  • Advertisement
Advertisement