dhruba
dhruba

Reputation: 281

logout using cookies php

This is the way i set cookies for authentication purpose, but i need a logout function to destroy those cookies...and send back to index page, please help me out??

<?php
require_once('Template.php');
require_once('common/common.php');

$mes="";
if($value['m']==1)
 {
    $mes="Invalid Username / Password.";
    setcookie("USERNAME", "", time()-3600);
 }

$template =& new Template('html/login.html');
$template->AddParam('mes',$mes);
$template->EchoOutput();

?>

actually i forgot to post the login authentication code...

<?php
 require_once('class/User.php');
 require_once('common/common.php');
 $user= new User();

 $user->getUser($value['username'],$value['password']);

 if($user->ID != null){
setcookie("USERNAME", $user->USERNAME);
header("Location:adminhome.php");

  }
 else
 {
 header("Location:index.php?m=1");
 }
  ?>

Upvotes: 1

Views: 6336

Answers (3)

vikki
vikki

Reputation: 2814

logout.php:

setcookie("USERNAME" , '' , time()-50000, '/');
header("Location: index.php");
exit;

add a link to logout.php. logout.php should contain the code above.

setcookie("USERNAME" , '' , time()-50000, '/');

this destroys the cookie.

header("Location: index.php");
exit;

this redirects the user to index.php

I've removed the if statement cause i've realised it's not useful here

Upvotes: 1

Sudheer
Sudheer

Reputation: 728

You might want to add an exit(); statement after the call to Header().

Upvotes: 0

user1026996
user1026996

Reputation:

You are currently unsetting the cookie in your example. Also, setting a cookie with no value is the same as deleting it. Then simply redirect to your landing page after logout.

setcookie( 'cookie_name'); // deletes the cookie named cookie_name
Header("Location: url.com");

Upvotes: 0

Related Questions