Reputation: 2465
I have a cookie I set when a user logs into my website and I also have a session ID for them, the only trouble right now is that the cookie I set doesn't seem to be able to be accessed. Although I can access the PHPSESSID cookie.
User Login:
$_SESSION['user_is_loggedin'] = 1;
setcookie('usr', $usr->username, time()+3600*24*7, '/');
Accessing the cookie:
if(isset($_SESSION['user_is_loggedin']) && $_SESSION['user_is_loggedin'] == 1 ) {
echo $_COOKIE['usr'];
error:
Undefined index: usr
I understand why it would error without setting the cookie, but it still errors even WITH the cookie set.
EDIT It seems that after I refresh the page the session exists but after that it clears the session. Do sessions not carry over after a page refresh?
Upvotes: 2
Views: 118
Reputation: 36511
Make sure you are calling session_start();
and setting your cookie at the top of your script, not doing so will send headers and cause session_start()
and setcookie()
to fail.
Also, as @MiloLaMar pointed out, if you had error_reporting turned on you would've seen that it was failing:
ini_set('display_errors', 1);
error_reporting(E_ALL);
Upvotes: 2