Possible Duplicate:
(PHP) How to destroy the session cookie correctly?
I'm doing a shopping cart in PHP. I am using session_id()
to identify individual users. After saving the product order, I need to destroy the session_id
. How will I do that?
Possible Duplicate:
(PHP) How to destroy the session cookie correctly?
I'm doing a shopping cart in PHP. I am using session_id()
to identify individual users. After saving the product order, I need to destroy the session_id
. How will I do that?
use a session_regenerate_id()
. It will change the session id, automatically invalidating the old one
For a good destroy, use the following on logout:
session_start();
session_regenerate_id();
session_destroy();
Not sure what you mean, but this is how you destroy a session
unset( $_SESSION['id'] )
use
session_regenerate_id();
instead. It will regenerate the Session ID.
See more information at: http://php.net/manual/en/function.session-regenerate-id.php
Using session ID is not the best way to go (although you could use session_regenerate_id()
to create new session ID) because you loose contents of cart if user logs out (for example he will start preparing order in work and would want to finish it home).
Much better, flexible and easier to control would be creating table:
CREATE TABLE carts (
`id` INT,
`user_id` INT
)
Give this a try..
page1.php
<?php
session_start();
$_SESSION['userID'] = 123;
?>
<a href="logout.php">Logout</a>
logout.php
<?php
session_start(); // start the session before using it
session_destroy(); // delete the current session
//$_SESSION['userID'] = 123; // <---- NO LONGER EXISTS
echo "Session destroyed!"; // inform the user of the changes
?>
Or you could just have the session_destroy(); in with your code