2

I am currently developing a PHP website. I am trying to set a cookie which will store a user id. This is for a logging in system and I need the user id to be able to add it into different tables in a MySQL database.

When the user successfully logs in it creates a PHP session and a cookie and then uses a meta tag refresh to direct to the index page.

if ($row = mysql_fetch_array($result))
{
    setcookie('id', $row['use_id']);
    $_SESSION['user'] = $username;
    setcookie('userID', $row['use_id']);
    echo '<meta http-equiv="refresh" content="0;url=../index.php">';
}

If I remove the meta refresh and just echo the cookie using $_COOKIE['userID'] it works fine but when I try and use $_COOKIE['userID'] on the index.php page after it has done the meta refresh it isn't displaying anything.

6
  • 2
    Why not send the refresh as a header, too? No need to use a meta tag - if you can send cookie headers, you can also send refresh headers. Apr 2, 2011 at 21:17
  • @ThiefMaster If I use the header redirect it errors because I am outputting HTML to the screen
    – Boardy
    Apr 2, 2011 at 21:18
  • 1
    What browser are you testing in? Some browsers only allow you to set cookies in a POST request.
    – Naatan
    Apr 2, 2011 at 21:22
  • @Naatan I've tried Google Chrome Development Build, normal google chrome and IE9
    – Boardy
    Apr 2, 2011 at 21:23
  • Can you show us the response headers for the page that sets the cookie?
    – Naatan
    Apr 2, 2011 at 21:42

2 Answers 2

5
setcookie('userID', $row['use_id'], 0, "/");

Edit: zero for expire time.

Your cookie is not visible for index.php because it has been set from script in subdirectory. So it is not visible for pages on upper levels. You should set cookie path (4th parameter) to "/" and the cookie will be available within the entire domain.

This was the reason why your cookie was not visible to index.php.

header('Location: ../index.php');
                  ^^^

You have redirected page to upper directory level, but your cookie was not set for entire domain.

4
  • If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes). Refreshing the page doesn't close the browser, i guess that wouldn't be a solution..
    – Rihards
    Apr 2, 2011 at 21:20
  • This makes no difference, when you dont set a time the cookie counts for the browsing session.
    – Naatan
    Apr 2, 2011 at 21:21
  • 1
    Excellent this works fine. It needs to have of all of these parameters to work. If I remove any of the last 2 parameters it for some reason it stops working. But with them is fine. Thanks again
    – Boardy
    Apr 2, 2011 at 21:29
  • 1
    Last parameter is very important! It allows cookie-visibility for all pages within entire domain.
    – Wh1T3h4Ck5
    Apr 2, 2011 at 21:31
2

if the index pages directory is upper than the directory which you set the cookie from, the cookie will not be available in the index page. And i suggest you to use location header for redirections. Hope this will help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.