Question

When I'm trying logout with: session_destroy(); then it worked. But the problem is, I have also a shopping cart with sessions. So session_destroy(); isn't a option for me. When I'm using the unset($_SESSION['username']); function it seems working. But when I go back to my accountpage, I'm just back logged in. My logout script looks like this:

<?php
unset($_SESSION['username']);
unset($_SESSION['id']);

header('Location: index?page=home');
?>

I checked with this piece of code of a user is logged in. (Can somebody tell me also or this is safe?)

<?php if (!$_SESSION['username'] && !$_SESSION['id']){ ?>
<?php header ('Location: index?page=home'); ?>
<?php } ?>

This is the info that you need (I think). If you need more, please ask me.

So, my question in the short version: How can I destroy my session without destroying my shopping cart session?

Thanks guys.

Was it helpful?

Solution

Can you try session_write_close() here? There is a PHP configuration which is supposed to automatically do this for you after the script ends but your PHP might be configured differently.

<?php
unset($_SESSION['username']);
unset($_SESSION['id']);

session_write_close();

header('Location: index?page=home');
?>

For the sake of debugging could you try this:

<?php

echo $_SESSION['id'].' - '.$_SESSION['username'];

if(!$_SESSION['username'] && !$_SESSION['id']){
    header ('Location: index?page=home');
}

?>

OTHER TIPS

Might it be possible, that you've just forgot the session_start(); function call in your logout script?

<?php
session_start();

unset($_SESSION['username']);
unset($_SESSION['id']);

header('Location: index?page=home');

in easiest way use

<?php
session_start();
if(empty($_SESSION['user']) && empty($_SESSION['id']))
{
echo "please login";

}

and unset use to unset the variable and session_destroy for destroy session completly, so i suggest you should use both as:

<?php
session_start();
unset($_SESSION['user']);
unset($_SESSION['id']);
session_destroy();
header("location:yourpage.com");
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top