SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2009
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem Setting Cookies

    Hello,

    I have a login screen and when the user clicks the login button it goes
    to my php script where I check the database to validate the login creds.
    Then I want to set a cookie. I am having such a diffcult time doing this.
    I was wondering if something could help me out.

    here is part of my code - anyways the cookie is not getting set.
    what have I done wrong?


    $sUser = $_GET['user'];
    $sPwd = $_GET['pwd'];

    setcookie("user", $sUser, time()+3600);
    setcookie("user", $sPwd, time()+3600);


    if (isset($_COOKIE["user"]))
    echo "Welcome " . $_COOKIE["user"] . "!<br />";
    else
    echo "Welcome guest!<br />";

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Couple of things...

    1)

    Cookie are not available immediately after setting them, it requires a new request.

    Basically you need to redirect them or reload the page in order to see the value in $_COOKIE.

    2)
    If that's not your problem then try adding the 4th and 5th parameters for the cookie

    setcookie('user', $sUser, time() + 3600, '/', $_SERVER['HTTP_HOST'])

    will usually do the job.

    3)
    Also a cookie is a single plain text string, therefore if you set $sUser, then do another setcookie with $sPwd, it will only store the pwd, so you need to serialize if you need to store an array of values. Or at the very least separate them with a predefined character, like | (pipe) and split it internally.

    4)

    Storing passwords in cookies isn't really good practice of security, this is why most sites only store your username and make you re-enter your password.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •