March 22, 201313 yr hi guys, I have a webpage that allows users to login and sign in automatically if they check the "sign me in" check box. I use cookie to store their username and they are automatically redirected to their profile by the username stored in cookie. I want to remove this cookie when they click on logout button. this is my logout function which is triggered when the logout is clicked, but it does not remove cookie. public function logoutAction() { session_start(); unset($_COOKIE['username']); setcookie("username",' ',time()-10); session_destroy(); header("location:/virtualsociety/register/stage1");} and this function is called through ajax call. what is the problem? I guess because it is called through ajax in background with out involving browser. tnx alot. Edited March 22, 201313 yr by stallman3738
March 22, 201313 yr For 1 u dont need a session start or destroy for removing a cookie try this function public function logoutAction() { if(isset($_COOKIE['username'])): setcookie("username","",time()-3600); header("location:/virtualsociety/register/stage1"); exit; else: die('You are not logged in there for you cannot logout'); endif; } basically what i tried us setting the time back further then just 10 normally u would set the time back atleast 3600 which is 1 hour
March 23, 201313 yr Author For 1 u dont need a session start or destroy for removing a cookie try this function public function logoutAction() { if(isset($_COOKIE['username'])): setcookie("username","",time()-3600); header("location:/virtualsociety/register/stage1"); exit; else: die('You are not logged in there for you cannot logout'); endif; } basically what i tried us setting the time back further then just 10 normally u would set the time back atleast 3600 which is 1 hour I saw something very strange! I change that function to this : public function logoutAction() { header("location:/virtualsociety/registration/com/views/logout.php"); } and this is logout.php page : if(isset($_COOKIE['username'])) { echo $_COOKIE['username']; unset($_COOKIE['username']); setcookie("username"," ",time()-3600*30*12); exit(); } else { echo "No cookie"; exit(); } the output is No cookie but when I visit stage1 page, which is login page and has the following code, it outputs the cookie and redirects to profile !!! session_start(); if(isset($_COOKIE['username'])) { echo $_COOKIE['username']; $dbuser = new \registration\com\models\database(); if($dbuser->autologin()==true) //checks if $_COOKIE['username'] corresponds to any existing user header("location:/virtualsociety/home/viewProfile/".$_COOKIE['username']); } WHEN I CHECK THE BROWSER COOKIE, USERNAME IS STILL IN THERE WHILE PHP IN THIS CODE SAYS THERE IS NO COOKIE print_r($_COOKIE); THIS IS OUTPUT : Array ( [phpSESSID] => v7h79f2ajd4hi60apcu8qu6595 ) Edited March 23, 201313 yr by stallman3738
March 23, 201313 yr Author I have set : setcookie("username",' ',time()-3600 * 31 * 12); and problem solved tnx
Create an account or sign in to comment