TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-03-2008, 05:36 PM   #1 (permalink)
The Contributor
 
Join Date: Jan 2008
Posts: 87
Thanks: 49
StevenF is on a distinguished road
Default Best Way To Delete A Cookie?

Hello,

I'm having some problems with my shopping cart scripts. When a user logs in, the following code is executed:


PHP Code:
//Start the session
                
session_start();
                
//Create session variable
                
session_register("user"); 
                
//let's grab the session ID for those who don't have cookies
                
$id session_id(); 
            
                
$url "Location: myaccount.php?sid=" $id;
                
header($url); 
Now when you logout, it does the following:

PHP Code:
//let's completely terminate the session and bring them to login page
                    
session_start(); 
                    
session_unset();
                    
session_destroy();
                     echo (
"Logging Out...");
                    
header('Refresh: 1; url=index.php'); 
When you try to log in with two different accounts, both are being assigned the same session ID. I think this is because the cookie was being deleted server side, but it was still remaining on the clients machine? For some reason

PHP Code:
session_unset();
session_destroy(); 
wasn't deleting the cookie from the machine.

Would it be OK to add a bit of JavaScript after "session_destroy which deleted the cookie? I was told I couldn't delete it in PHP because there has already been some outputted after the header or something - sorry, not explained very well.

I was each user to be assigned a different session_id because I am using that to select the products in their cart.

Thanks,
Steven
__________________
My Personal and Photo Blog
StevenF is offline  
Reply With Quote
Old 03-03-2008, 06:22 PM   #2 (permalink)
The Contributor
 
abiko's Avatar
 
Join Date: Feb 2008
Location: Croatia
Posts: 90
Thanks: 4
abiko is on a distinguished road
Default

to delete a cookie:
you have your cookie code
PHP Code:
// Set cookie
setcookie("cookieName"$valuetime()+3600);  
// Unset Cookie
setcookie("cookieName"$valuetime()-36000); 
The same could be used - cookieName = PHPSESSID amd unset it.


From session_destroy() @ php.net manual
session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie.

In order to kill the session altogether, like to log the user out, the session id must also be unset. If a cookie is used to propagate the session id (default behavior), then the session cookie must be deleted. setcookie() may be used for that.
__________________
Back from sysadmins to the programmers.
Send a message via ICQ to abiko Send a message via MSN to abiko
abiko is offline  
Reply With Quote
The Following User Says Thank You to abiko For This Useful Post:
StevenF (03-03-2008)
Old 03-03-2008, 07:09 PM   #3 (permalink)
The Contributor
 
Join Date: Jan 2008
Posts: 87
Thanks: 49
StevenF is on a distinguished road
Default

Oh so, so would something like this work instead?

If login = true

PHP Code:
//Start the session
session_start();
//Create session cookie
// Set cookie
setcookie("loggedin""TRUE"time()+(3600 24));
//let's grab the session ID for those who don't have cookies
$id session_id(); 
$url "Location: myaccount.php?sid=" $id;
header($url); 
else

PHP Code:
session_start(); 
session_unset();
session_destroy();
unset(
$id);
// expire cookie
setcookie ("loggedin"""time() - 3600);  
echo (
"Logging Out...");
header('Refresh: 1; url=index.php'); 
$id is storing the session_id.

Would that work? Also, what would replace $value?

Edit: That wouldn't work. As I said in the first post:

Quote:
Originally Posted by php.net
Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.
__________________
My Personal and Photo Blog

Last edited by StevenF : 03-03-2008 at 08:03 PM.
StevenF is offline  
Reply With Quote
Old 03-03-2008, 09:54 PM   #4 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

That should work fine. Since you're not setting any content on the cookie and the timer in negative, it's automatically a false cookie. Plus, almost every browser including Netscape and Internet Explorer delete out-of-date cookies. Otherwise, it's not a problem since you can make your system check for cookies and them validate the timestamp given.

To sum it up once again, that should work fine. And oh yeah, to the quote. Think of setting (and unsetten) of cookies as headers. It's basically the same rule, before ANY output. I haven't found a good method to unset it in-script, but I do it with a post variable.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
The Following User Says Thank You to ReSpawN For This Useful Post:
StevenF (03-03-2008)
Old 03-03-2008, 10:33 PM   #5 (permalink)
The Contributor
 
Join Date: Jan 2008
Posts: 87
Thanks: 49
StevenF is on a distinguished road
Default

Quote:
Originally Posted by ReSpawN View Post
That should work fine. Since you're not setting any content on the cookie and the timer in negative, it's automatically a false cookie. Plus, almost every browser including Netscape and Internet Explorer delete out-of-date cookies. Otherwise, it's not a problem since you can make your system check for cookies and them validate the timestamp given.

To sum it up once again, that should work fine. And oh yeah, to the quote. Think of setting (and unsetten) of cookies as headers. It's basically the same rule, before ANY output. I haven't found a good method to unset it in-script, but I do it with a post variable.
But that's my problem; the above code exists half way through my script. I was thinking it would be possible to set the cookie in PHP as shown, then delete it using JavaScript?
__________________
My Personal and Photo Blog
StevenF is offline  
Reply With Quote
Old 03-03-2008, 10:45 PM   #6 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 264
Thanks: 2
TlcAndres is on a distinguished road
Default

ob_start();
__________________
"What everyone seems to forget is that while knowledge certainly is something - it's the implementation of knowledge that brings power" - Andres Galindo.
TlcAndres is offline  
Reply With Quote
Old 03-03-2008, 11:20 PM   #7 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

I know this has already been answered but here's what it says at php.net.

PHP: session_destroy - Manual
Quote:
In order to kill the session altogether, like to log the user out, the session id must also be unset. If a cookie is used to propagate the session id (default behavior), then the session cookie must be deleted. setcookie() may be used for that.

...

PHP Code:
<?php
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();

// Unset all of the session variables.
$_SESSION = array();

// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (isset($_COOKIE[session_name()])) {
    
setcookie(session_name(), ''time()-42000'/');
}

// Finally, destroy the session.
session_destroy();
?>
...

Note: Only use session_unset() for older deprecated code that does not use $_SESSION.
__________________
Eric
wGEric is offline  
Reply With Quote
The Following User Says Thank You to wGEric For This Useful Post:
StevenF (03-04-2008)
Old 03-03-2008, 11:47 PM   #8 (permalink)
The Contributor
 
Join Date: Jan 2008
Posts: 87
Thanks: 49
StevenF is on a distinguished road
Default

I have it so that it removes the cookie when you log out, but for some reason, it's still logging two different people in with the same session id :(

I need to be able to reset the session_id() without closing the browser, is that possible?
__________________
My Personal and Photo Blog

Last edited by StevenF : 03-04-2008 at 12:25 PM.
StevenF is offline  
Reply With Quote
Old 03-04-2008, 01:38 PM   #9 (permalink)
The Contributor
 
Join Date: Jan 2008
Posts: 87
Thanks: 49
StevenF is on a distinguished road
Default

No matter, I thought of a better solution:

When a user logs in, their username is stored in a cookie. I then store that cookie in a $username variable on the products page. This allows me to $_GET the username from the url and insert it into the cart table when they select an item. In turn allowing me to display all the items added by that user.

Took a bit of thinking, but I got it in the end! Now I need to check if a row already exists, and if it doesn't, run the query.
__________________
My Personal and Photo Blog
StevenF is offline  
Reply With Quote
Old 03-04-2008, 02:26 PM   #10 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default

You should try putting session_regenerate_id().
Gareth is offline  
Reply With Quote
The Following User Says Thank You to Gareth For This Useful Post:
StevenF (03-04-2008)
Old 03-04-2008, 03:43 PM   #11 (permalink)
The Contributor
 
Join Date: Jan 2008
Posts: 87
Thanks: 49
StevenF is on a distinguished road
Default

Quote:
Originally Posted by Gareth View Post
You should try putting session_regenerate_id().
I was actually looking at that earlier but never put it into practice. The method I posted above works much better. I originally wanted to store the session_id() in the cart table, and then use that to identify each order. But ever time the person visited the page they would be using a different ID and they would have to start their order again. I like my new method and it's working as intended :)

Thanks all,
Steven
__________________
My Personal and Photo Blog
StevenF is offline  
Reply With Quote
Old 03-08-2008, 03:36 PM   #12 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default

Sorry for a bump but I meant to use session_regenerate_id() when the user logs in. This can help to prevent session hijacking (it isn't 100% effective, mind you!).

Glad you got it working as intended!

Gareth
Gareth is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:54 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design