Jump to content

Recommended Posts

I have a top nav portion of my site called header.php. This site displays "Logged in" or "Sign in" based on whether a cookie is set or not. The file header.php is included on all pages.

 

When I got to my logout.php page, however, I want the cookie to be destroyed BEFORE the header is loaded so that the header shows "Sign in" instead of "Logged in." This doesn't seem to be happening so I have to refresh the page and then it displays properly.

 

Here's the code:

<?php
        setcookie("user", "", time() - 3600);
        include("header.php");
?>

 

The functionality is there but I'd like it to display properly. Any suggestions on how to get around this? Thanks!

Link to comment
https://forums.phpfreaks.com/topic/199501-php-include-and-cookies/
Share on other sites

The $_COOKIE variables are set when the browser requests a page. setcookie() sends the header to the browser to get it to change the saved cookie in the browser.

 

You have a couple of choices -

 

1) After the setcookie(), perform a redirect to the same page (this will insure that the cookie was actually set to the new value.)

 

2) You can set the $_COOKIE variable to the value you want in your code (this has the disadvantage of not truly showing the actual value in case the setcookie() fails.)

 

However, you should not use the existence or absence of a cookie to tell you someone is logged in. All I would need to do is create a cookie that says I am you and I can visit your site and do anything on it that you can do. You should use the cookie to identify the visitor only, through a unique value that cannot easily be guessed or reverse engineered, and determine the logged in/logged out status through a value that is kept on the server only (so that if you log out, no one that gets ahold of your cookie values can appear to be logged in without logging in using the correct username and password.)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.