Jump to content

Session with cookie logout


twsowerby

Recommended Posts

Hi all,

 

Just getting my head around sessions and came across a stumbling block.

 

I have the following code which compares a login form to a database value and starts a new session. It also stores a cookie containing the session id. I got this code from a tutorial and understand how it works.

 

<?php
// Check if the information has been filled in
if($psEmail == '' || $psPassword == '') {

// No login information
header('Location: login.php?refer='.urlencode($psRefer));

} else {

// Authenticate user
$hDB = mysql_connect('localhost', '', '');
mysql_select_db('fyp', $hDB);

$sQuery = "
Select iUser, MD5(UNIX_TIMESTAMP() + iUser + RAND(UNIX_TIMESTAMP())) sGUID
From tblUsers
Where sEmail = '$psEmail'
And sPassword = password('$psPassword')";

$hResult = mysql_query($sQuery, $hDB);

if(mysql_num_rows($hResult)) {

$aResult = mysql_fetch_row($hResult);

// Update the user record
$sQuery = "
Update tblUsers
Set sGUID = '$aResult[1]'
Where iUser = $aResult[0]";

mysql_query($sQuery, $hDB);

// Set the cookie and redirect
setcookie("session_id", $aResult[1]);

if(!$psRefer) $psRefer = 'index.php';
header('Location: '.$psRefer);

} else {

// Not authenticated
header('Location: login.php?refer='.urlencode($psRefer));
}
}
?> 

 

I'm not sure how to do the logout part of this though, how do you end the session? presumably I also have to remove the cookie that stores the session ID.

 

Any help would be appreciated, thanks.

 

Tom

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.