Jump to content

how to destroy sessions in php?


ypran

Recommended Posts

Are you using session_destroy()?

 

On the top of each of my pages that are protected by a login, I usually include a file that has some code like the following:

<?php
if (isset($_SESSION['something']))
{
// show page
}
else
{
header("Location: index.php");
exit;
}
?>

 

It works very well for what I use it for.

Are you using session_destroy()?

 

On the top of each of my pages that are protected by a login, I usually include a file that has some code like the following:

<?php
if (isset($_SESSION['something']))
{
// show page
}
else
{
header("Location: index.php");
exit;
}
?>

 

It works very well for what I use it for.

 

You could just use:

if(!isset($_SESSION['something'])){
header("Location: index.php");
exit;
}

 

And it wouldn't show the page.

Delete the cookie too

 

From php.net:

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.