Jump to content

Recommended Posts

hi, my cookie is really causing me problems. when i set my cookie in the login page i try to make it available throughout th website

<?php
// if login is ok then we add a cookie 
$_POST['username'] = stripslashes($_POST['username']); 
$hour = time() + 3600; 
setcookie(ID_my_site, $_POST['username'], $hour, "/"); 
setcookie(Key_my_site, $_POST['pass'], $hour, "/");

header("Location: http://localhost/main/"); 
?>

it then directs to the main page.

 

when i'm on other pages the cookie shows as it is suppose to but the problem is when i log out.

 

my header file

contains the logout button

and this code

<?php
if (isset($_POST['logout'])) { 
$past = time() - 3900; 
//this makes the time in the past to destroy the cookie 
setcookie("ID_my_site", $username, $past); 
setcookie("Key_my_site", $pass, $past); 

   // Create the URL string
   $url = "http://localhost/"; 
   
   // Finall Echo the meta tag
   echo('<meta HTTP-EQUIV="REFRESH" content="0; url='.$url.'">');
}
?>

 

when i try to log out from my main page it kills the cookie, however when i try to log out from any other page...profile, friends etc. i get redirected to the main page, of which if i then try to log out from that same main page it does so

 

Link to comment
https://forums.phpfreaks.com/topic/148976-cookie-availability/
Share on other sites

Should it be $_POST of logout or $_GET?

 

Alternatively you can use $_REQUEST['logout'] and that checks for both and if that is set then the code runs. Since this is a logout page, I think that would be kosher.

 

How do they logout from every other page? A button or a link with ?logout=true at the end?

 

setcookie("ID_my_site", false); 
setcookie("Key_my_site", false); 

 

I would also use that for your logout cookies, as setting the value to false attempts to destroy to cookie.

Link to comment
https://forums.phpfreaks.com/topic/148976-cookie-availability/#findComment-782238
Share on other sites

From the setcookie() page in the php manual -

 

Cookies must be deleted with the same parameters as they were set with.

 

If you are using the path parameter in the setcookie() statements to set the cookie, you must use the same parameters when you delete the cookie.

Link to comment
https://forums.phpfreaks.com/topic/148976-cookie-availability/#findComment-782240
Share on other sites

could i do something like this

<?php
if (isset($_POST['logout'])) { 
$past = time() - 3900; 
//this makes the time in the past to destroy the cookie 
setcookie("ID_my_site",  $_COOKIE['ID_my_site'], $past, "/"); 
setcookie("Key_my_site", $_COOKIE['Key_my_site'], $past, "/"); 

   // Create the URL string
   $url = "http://localhost/"; 
   
   // Finall Echo the meta tag
   echo('<meta HTTP-EQUIV="REFRESH" content="0; url='.$url.'">');
}
?>

 

ill try the request as well

 

post is coming from button cause this is in the header file

 

Link to comment
https://forums.phpfreaks.com/topic/148976-cookie-availability/#findComment-782250
Share on other sites

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.