Pi_Mastuh Posted January 22, 2007 Share Posted January 22, 2007 I've got a code that creates cookies to keep a user logged in, but how do I delete them for if they want to logout? If the cookie is found it re-directs to another page and I tried setting new cookies to expire an hour ago but it's not deleting the old one, which keeps the user from logging out. Here's my code:[code] <?php if(isset($_COOKIE['preuserID']) && isset($_COOKIE['preuserName'])){ $_SESSION['preuserID'] = $_COOKIE['preuserID']; $_SESSION['preuserName'] = $_COOKIE['preuserName']; header("Location: ../reg/home.php"); }[/code] Quote Link to comment Share on other sites More sharing options...
marcus Posted January 22, 2007 Share Posted January 22, 2007 [code=php:0]unset($_COOKIE['value']);//or give it a bad value$_COOKIE['value'] = false;[/code] Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted January 22, 2007 Author Share Posted January 22, 2007 so it would be unset($_COOKIE['preuserID']);unset($_COOKIE['preuserName']);? Quote Link to comment Share on other sites More sharing options...
marcus Posted January 22, 2007 Share Posted January 22, 2007 Yep, it'll destroy the cookie.http://us3.php.net/manual/en/function.unset.php Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted January 22, 2007 Author Share Posted January 22, 2007 it's not working. :-\ Quote Link to comment Share on other sites More sharing options...
marcus Posted January 22, 2007 Share Posted January 22, 2007 Worked for me. Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted January 22, 2007 Author Share Posted January 22, 2007 Did I do something wrong? I put it on the logout page. The logout page re-directs to the main index.php page, which re-directs to home.php IF it finds the cookies. I hit logout and it's just going to home.php, but it shouldn't be finding the cookies. Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted January 22, 2007 Author Share Posted January 22, 2007 Here's all of my code[code]<?php //Create the cookie after the user logs insetcookie("preuserID", $_SESSION['preuserID'], time()+60*60*24*100, "/"); setcookie("preuserName", $_SESSION['preuserName'], time()+60*60*24*100, "/"); header("Location: ../home.php");[/code][code]<?php // Check if user has been remembered and re-direct if(isset($_COOKIE['preuserID']) && isset($_COOKIE['preuserName'])){ $_SESSION['preuserID'] = $_COOKIE['preuserID']; $_SESSION['preuserName'] = $_COOKIE['preuserName']; header("Location: ../reg/home.php"); }[/code][code]<?php //Log the user outunset($_COOKIE['preuserID']);unset($_COOKIE['preuserName']);[/code] Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 22, 2007 Share Posted January 22, 2007 Check out example 2 on the setcookie page.http://us2.php.net/setcookieIt goes over how to delete cookies. Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted January 22, 2007 Author Share Posted January 22, 2007 I tried that, it wasn't working either. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 22, 2007 Share Posted January 22, 2007 Are you using Firefox? Open Tools, Options, Privacy. Click show cookies and find your cookies. See what they're set to. Then try your logout and see again. Setting them to nothing and with an expiration date in the past should work. Why do you say it's not working? Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted January 22, 2007 Author Share Posted January 22, 2007 I'm using IE. And i don't know why it wasn't working, it's still re-directing and it only does that if the cookies are found. Quote Link to comment Share on other sites More sharing options...
printf Posted January 22, 2007 Share Posted January 22, 2007 You can't unset a $_COOKIE with unset();, it will only remove the scope of the local GLOBAL variable, not remove it from the browser instance!Also don't use setcookie() if your redirecting, because not all browsers will set the cookie, if a redirect is issued in the same header! Use header(Set-Cookie:...) to set a cookie when redirecting, it's 100% cross browser safe, which setcookie() is not!printf Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 22, 2007 Share Posted January 22, 2007 So, take a look in Firefox, to help ;) Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted January 22, 2007 Author Share Posted January 22, 2007 [quote author=printf link=topic=123447.msg510246#msg510246 date=1169435565]You can't unset a $_COOKIE with unset();, it will only remove the scope of the local GLOBAL variable, not remove it from the browser instance!Also don't use setcookie() if your redirecting, because not all browsers will set the cookie, if a redirect is issued in the same header! Use header(Set-Cookie:...) to set a cookie when redirecting, it's 100% cross browser safe, which setcookie() is not!printf[/quote]How do I do that? Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted January 22, 2007 Author Share Posted January 22, 2007 Like this?header(setcookie ("preuserID", "", time() - 60*60*24*100)(setcookie ("preuserName", "", time() - 60*60*24*100)("Location: ../"))); Quote Link to comment Share on other sites More sharing options...
printf Posted January 22, 2007 Share Posted January 22, 2007 You can do that way, but I most times create a function based control so I can delete all cookies at once, or set a cookie using header instead of setcookie() that doesn't work 100% across all browsers.example...[code]<?phpfunction cookie_remove (){ if ( ! empty ( $_COOKIE ) ) { foreach ( $_COOKIE AS $k => $v ) { set_cookie ( $k ); } }}function set_cookie ( $name, $value = '', $expire = ( time ( ) - 86400 ), $path = '/', $domain = $_SERVER['SERVER_NAME'], $secure = false ){ header ( 'Set-Cookie: ' . $name . '=' . $value . '; expires=' . ( $expire == 0 ? 0 : gmdate ( 'D, d-M-Y H:i:s \G\M\T', $expire ) ) . '; path=' . $path . '; domain=' . $domain . ( $secure ? '; secure;' : '' ), false );}?>[/code]Then to use the functions![code]<?php// removes all cookies!cookie_remove ();// set a cookie to last for this browser sessionset_cookie ( 'auth', 'true', 0 );// set a persistent cookie to last for a yearset_cookie ( 'auth', 'true', ( time () + 31536000 ) );// set a persistent cookie to last for a year, valued for a certain path onlyset_cookie ( 'auth', 'true', ( time () + 31536000 ), '/admin/' );// remove a single cookieset_cookie ( 'cookie_name_to_remove' );?>[/code]printf Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted January 22, 2007 Author Share Posted January 22, 2007 I put it in the page and got Parse error: parse error, unexpected '(' in /homepages/20/d175171605/htdocs/reg/logout.php on line 19Did I do it right?[code]function cookie_remove (){ if ( ! empty ( $_COOKIE ) ) { foreach ( $_COOKIE AS $k => $v ) { set_cookie ( $k ); } }}function set_cookie ( $name, $value = '', $expire = ( time ( ) - 86400 ), $path = '/', $domain = $_SERVER['SERVER_NAME'], $secure = false ){ header ( 'Set-Cookie: ' . $name . '=' . $value . '; expires=' . ( $expire == 0 ? 0 : gmdate ( 'D, d-M-Y H:i:s \G\M\T', $expire ) ) . '; path=' . $path . '; domain=' . $domain . ( $secure ? '; secure;' : '' ), false );}set_cookie ( 'preuserID' );set_cookie ( 'preuserName' );[/code] Quote Link to comment Share on other sites More sharing options...
printf Posted January 22, 2007 Share Posted January 22, 2007 Sorry about that.... (the complete working script would be something like so)[code]<?define ( 'COOKIE_PATH', '/' );define ( 'COOKIE_EXPIRE', time () - 86400 );define ( 'COOKIE_SECURE', false );// for for global domain access use .your_domain.com, instead of $_SERVER['SERVER_NAME']define ( 'COOKIE_DOMAIN', $_SERVER['SERVER_NAME'] );function cookie_remove (){ if ( ! empty ( $_COOKIE ) ) { foreach ( $_COOKIE AS $k => $v ) { set_cookie ( $k ); } }}function set_cookie ( $name, $value = '', $expire = COOKIE_EXPIRE, $path = COOKIE_PATH, $domain = COOKIE_DOMAIN, $secure = COOKIE_SECURE ){ header ( 'Set-Cookie: ' . $name . '=' . $value . '; expires=' . ( $expire == 0 ? 0 : gmdate ( 'D, d-M-Y H:i:s \G\M\T', $expire ) ) . '; path=' . $path . '; domain=' . $domain . ( $secure ? '; secure;' : '' ), false );}set_cookie ( 'preuserID' );set_cookie ( 'preuserName' );?>[/code]printf Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted January 22, 2007 Author Share Posted January 22, 2007 It's just re-directing back to home.php again, which means the cookies aren't being deleted ??? Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 22, 2007 Share Posted January 22, 2007 Are you sure there isn't more in your script? Quote Link to comment Share on other sites More sharing options...
Pi_Mastuh Posted January 22, 2007 Author Share Posted January 22, 2007 My entire script, including all the stuff that was there before is[code]<?phpsession_start();include("secure/config.php"); session_unregister('preuserName'); session_unregister('preuserID'); session_unregister('image');session_destroy();define ( 'COOKIE_PATH', '/' );define ( 'COOKIE_EXPIRE', time () - 86400 );define ( 'COOKIE_SECURE', false );// for for global domain access use .your_domain.com, instead of $_SERVER['SERVER_NAME']define ( 'COOKIE_DOMAIN', $_SERVER['SERVER_NAME'] );function cookie_remove (){ if ( ! empty ( $_COOKIE ) ) { foreach ( $_COOKIE AS $k => $v ) { set_cookie ( $k ); } }}function set_cookie ( $name, $value = '', $expire = COOKIE_EXPIRE, $path = COOKIE_PATH, $domain = COOKIE_DOMAIN, $secure = COOKIE_SECURE ){ header ( 'Set-Cookie: ' . $name . '=' . $value . '; expires=' . ( $expire == 0 ? 0 : gmdate ( 'D, d-M-Y H:i:s \G\M\T', $expire ) ) . '; path=' . $path . '; domain=' . $domain . ( $secure ? '; secure;' : '' ), false );}set_cookie ( 'preuserID' );set_cookie ( 'preuserName' );header("Location: ../");exit;?>[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.