2hamed
2hamed

Reputation: 9047

Why `session_destroy()` does not work in PHP?

I have this problem with sessions in PHP which I have never been able to comprehend!
I have the following code in a PHP file:

session_start();
$sid = session_id();
session_unset();
session_destroy();
echo $sid;

Logically every time the it should give me different session IDs cause obviously I call session_destroy() but it just won't kill the session. It always gives me the same session ID!
Am I doing it wrong?

Upvotes: 1

Views: 3790

Answers (2)

lszrh
lszrh

Reputation: 1582

For me, only the following combination works perfectly to destroy a session completely:

session_destroy();
session_unset();
session_regenerate_id(true);

Edit: Did you try this:

<?php
session_start();
$sid = session_id();
echo $sid;
echo "<br />";

//destroy session
session_destroy();
session_unset();

//now start the new session
session_start();
session_regenerate_id(true);
$sid = session_id();
echo $sid;
?>

It work's perfectly.

Upvotes: 4

Dhruvisha
Dhruvisha

Reputation: 2530

seesion_id() concept

refer this link. It says If id is specified, it will replace the current session id. session_id() needs to be called before session_start() for that purpose.

There is a example in this states that

As of php 4.3.3 you get a notice when you start an session twice. You can use session_id() to detect if a session is already started. Before you can make a script, you must know that session_id() returns false when no session ID is known. So now we can make a script witch dectect if a session is started, and if it is not, it startes an session.

This is what you get then:
<?php
if(!session_id()){
 session_start();
}
?>

Upvotes: 2

Related Questions