Jump to content

Deleting Cookies


Pi_Mastuh

Recommended Posts

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]
Link to comment
Share on other sites

Here's all of my code

[code]<?php
//Create the cookie after the user logs in
setcookie("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 out
unset($_COOKIE['preuserID']);

unset($_COOKIE['preuserName']);[/code]
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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
Link to comment
Share on other sites

[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?
Link to comment
Share on other sites

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]<?php

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 );
}

?>[/code]


Then to use the functions!

[code]<?php

// removes all cookies!

cookie_remove ();

// set a cookie to last for this browser session

set_cookie ( 'auth', 'true', 0 );

// set a persistent cookie to last for a year

set_cookie ( 'auth', 'true', ( time () + 31536000 ) );

// set a persistent cookie to last for a year, valued for a certain path only

set_cookie ( 'auth', 'true', ( time () + 31536000 ), '/admin/' );

// remove a single cookie

set_cookie ( 'cookie_name_to_remove' );

?>[/code]


printf
Link to comment
Share on other sites

I put it in the page and got

Parse error: parse error, unexpected '(' in /homepages/20/d175171605/htdocs/reg/logout.php on line 19

Did 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]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

My entire script, including all the stuff that was there before is

[code]<?php

session_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]
Link to comment
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.