Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Logout.php 1

Status
Not open for further replies.

TrueJoker

Technical User
Jun 14, 2006
115
0
0
GB
I have created a members database that can be signed up to etc!

I have attempted to create some lougout script using online tutorials and text books as guides but none appear to work!

Code:
<? 
session_start(); 
unset($_session['pwd']); 
unset($_session['loginName']); 
session_destroy(); 
header("Location: logout.html") 
?>

is the code i am using at the moment! Can anyone tell me what i am doing wrong? or whether i am barking up completely the wrong tree?

--KEY--

'pwd' = Password of the user
'loginName' = Username
 
Variables in PHP are case sensitive and $_session is not the same as $_SESSION. You should capitalize those letters if it is the PHP session mechanism variables you're unsetting.
 
Ah thats got it working! Thank you =D Such a simple mistake to make DOH!
 
since you are destroying the session you should also (imo) send a cookie to the browser that overwrites the session cookie. do this before the session_destroy:

Code:
setcookie(session_name(),'', time() - 3600);
session_destroy();

and if you really are destroying the session why bother unsetting the session data first?

another neat way of doing it would be this:

Code:
//file logoout.php
session_start();
if(!empty(session_name())){
  $_SESSION = array();
  session_destroy();
  session_start();
  session_regenerate_id();
}

this will force a new cookie to the browser that has no data stored server side.
 
just to let you know that the last lt of code you provided comes up with errors :S

Code:
//file logoout.php
session_start();
if(!empty(session_name())){
  $_SESSION = array();
  session_destroy();
  session_start();
  session_regenerate_id();
}

does not appear to work! maybe its just me though thank you for your advice though it is very helpful
 
are you using a version of php before 4.3.2?
 
no i dont believe so! the error was:

Parse error: parse error, expecting `T_VARIABLE' or `'$'' in /home/httpd/vhosts/impa.net/httpdocs/logout.php on line 4

Code:
1. <?
2. //file logoout.php
3. session_start();
4. if(!empty(session_name())){
5.   $_SESSION = array();
6.   session_destroy();
7.   session_start();
8.   session_regenerate_id();
9. }
10. ?>
 
wsa being dim. sorry

Code:
<?php
error_reporting(E_ALL);
//file logoout.php
session_start();
if(session_name() ==""){   //[red]changed line[/red]
	$_SESSION = array();
	session_destroy();
	session_start();
	session_regenerate_id();
}
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top