session destroy for logout not removing row from mysql db

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

session destroy for logout not removing row from mysql db

Post by anjanesh »

I have this in login.php

Code: Select all

session_module_name("user");
session_set_save_handler("session_open","session_close","session_read","session_write","session_remove","session_gc");
session_start();
header("Location:MemberPage.php");
This worked fine - the session id etc is stored in the databse.
session_open,session_close,session_read,session_write,session_remove,session_gc functions are defined.

I have this in MemberPage.php
<a href="logout.php">Logout</a>

I have this in logout.php

Code: Select all

session_destroy();
But it gives this warning and the row (session id) stil remains in the databse.

Warning: session_destroy(): Trying to destroy uninitialized session in c:\inetpub\wwwroot\vlb\vlbjcas\logout.php on line 8

Giving this in the logout.php page also does not

Code: Select all

session_module_name("user");
session_set_save_handler("session_open","session_close","session_read","session_write","session_remove","session_gc");

How do I rectify this error ?

Thanks
Last edited by anjanesh on Wed Aug 03, 2005 1:24 am, edited 1 time in total.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Re: session destroy for logout not removing row from mysql d

Post by scorphus »

Hello anjanesh, greetings from Brazil!
anjanesh once wrote:Warning: session_destroy(): Trying to destroy uninitialized session in c:\inetpub\wwwroot\vlb\vlbjcas\logout.php on line 8
You have to initialize the section before you can destroy it. Try to add this line to logout.php:

Code: Select all

session_start();
session_destroy();
Hope that helps,
Scorphus.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

session_start is already initialized in login.php
If I have session_start again in logout.php it gives headers already sent
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Code: Select all

<?PHP 
session_start();
session_destroy();
echo "You are logged out!";
?>
copy that exact code into logout.php and tell us what it does
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

This is what happened when I gave session_start() in logout.php :

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\vlbjcas\logout.php:6) in C:\Program Files\Apache Group\Apache2\htdocs\vlbjcas\logout.php on line 10

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at C:\Program Files\Apache Group\Apache2\htdocs\vlbjcas\logout.php:6) in C:\Program Files\Apache Group\Apache2\htdocs\vlbjcas\logout.php on line 10

You are logged out!

session_start() is there in login.php which works successfully. On login it redirects to memberpage.php using header fn. In memberpage.php & all other php scripts after login I get the session id using $PHPSESSID. In memberpage.php there is a link to logout.php where I have given session_destroy() but does not get rid of the row created in the mysql table.
RadixDev
Forum Commoner
Posts: 66
Joined: Sun Mar 14, 2004 11:27 am
Location: U.K.

Post by RadixDev »

As you would probably know the below error normally means that you have either printed a text onto the page or used header() function:

Code: Select all

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\vlbjcas\logout.php:6) in C:\Program Files\Apache Group\Apache2\htdocs\vlbjcas\logout.php on line 10
The following code to destroy a sessoin might work (make sure that logout.php file starts with this code:

Code: Select all

<?php
session_start();
session_destroy();
session_unset('VARIABLE1');
session_unset('VARIABLE2');
// and so on
// or use unset($_SESSION['VARIABLE1']);
?>
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

session_start() is already started in login.php.
A session_start() when a session id is already present in the browser would give the headers sent etc error.

Code: Select all

session_destroy();
echo "You are logged out!";
session_unregister($PHPSESSID);
print($PHPSESSID.'<br>'.$str);
Warning: session_destroy(): Trying to destroy uninitialized session in C:\Program Files\Apache Group\Apache2\htdocs\vlbjcas\logout.php on line 13
You are logged out!8ff6cbec2a946fe7fa977c3269914b06

See here ? $PHPSESSID still exits when I already tried to destroy it.
Last edited by anjanesh on Wed Aug 03, 2005 1:25 am, edited 1 time in total.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

This is what login.php contains : the use defined fns write, remove rows etc from the table which is correct (sql syntax)

Code: Select all

<?php
session_module_name("user");
session_set_save_handler("session_open","session_close","session_read","session_write","session_remove","session_gc");
session_start();
header("Location:MemberPage.php");

function session_open($path,$name)...
function session_close()...
function session_read($id)...
function session_write($id,$data)...
function session_remove($id)...
function session_gc($life)...
?>
In MemberPage.php you got things like update your profile where I use $PHPSESSID to identify the user.
In MemberPage.php you got a link to logout.php.

In logout.php all I need is to remove the session id.
Thanks
Last edited by anjanesh on Wed Aug 03, 2005 1:26 am, edited 1 time in total.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Ok guys this did not complain :

Code: Select all

<?php
session_module_name("user");
session_set_save_handler("session_open","session_close","session_read","session_write","session_remove","session_gc");
session_start();
session_unset($PHPSESSID);
unset($PHPSESSID);
session_destroy();
header("Location:index.htm");
?>
All those session_read(), session_remove() are there.

It removed the row from the table. But $PHPSESSID still contains the same old session id.
Post Reply