Jump to content

Recommended Posts

Hi

 

I wrote this code, but it doesn't seem to destroy the session.

 

<?php
session_start(); 
if (!$_SESSION['access'])
  $_SESSION['access'] = 3;

echo 'before: ' . $_SESSION['access'] . '<br>';

session_destroy(); 

echo 'after: ' . $_SESSION['access'] . '<br>';

 

Anyone?

 

Kind regards

eggwart

Link to comment
https://forums.phpfreaks.com/topic/40837-destroy-sessions/
Share on other sites

You say:

 

If there is NO $_SESSION['access'] make it 3.

 

So it will display 3 if you are not logged in before running this test.

 

Snooble

 

To clarify:

 

<?php
session_start(); 

echo 'before: ' . $_SESSION['access'] . '<br>';

session_destroy(); 

echo 'after: ' . $_SESSION['access'] . '<br>';

 

Try that and then you'll see. Make sure you're logged in when running the script.

 

Snooble

Link to comment
https://forums.phpfreaks.com/topic/40837-destroy-sessions/#findComment-197724
Share on other sites

Sorry, that last bit of output shouldn't be there ie "Login "

This page stands by itself, and is run using Uniform Server 3.3.

 

I ran the code you posted and got

 

1:

2:

 

as expected. But howcome the variable persists as "3" after I've destroyed the session with this code:

 

<?php
session_start(); 
$_SESSION['access'] = 3;

echo 'before: ' . $_SESSION['access'] . '<br>';

session_destroy(); 

echo 'after: ' . $_SESSION['access'] . '<br>';

?>

 

Output:

 

before: 3

after: 3

Link to comment
https://forums.phpfreaks.com/topic/40837-destroy-sessions/#findComment-197743
Share on other sites

Hmmm. Guess I should read the manual...

 

 

Description

bool session_destroy ( void )

 

 

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.

 

Returns TRUE on success or FALSE on failure.

 

Example 1. Destroying a session with $_SESSION

 


<?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();
?>  


Link to comment
https://forums.phpfreaks.com/topic/40837-destroy-sessions/#findComment-197782
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.