2

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.

0

3 Answers 3

4

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');
}

?>
Sign up to request clarification or add additional context in comments.

2 Comments

When I'm using this function it's solved the problem: session_write_close(); Thanks for your help!
You're welcome! If you would like to know why this is occurring for you then please read my answer from this SO question: stackoverflow.com/questions/19119354/…
1

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');

2 Comments

I have a session_start(); at the top of my logout script.
@Appel because actually everything else looks sane… the only I can do is guessing…
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.