0

i am creating a website with login and logout and registration but an error appear every time i want to logout how to fix it i think the eroor is in the session this error make me crazy i did a lot of search about fixing the problem but i did not get any solution that help me.

logout.php

<?php
session_start();

session_destroy();

if(isset($_COOKIE['id_cookie'])){

setcookie("id_cookie", "", time()-50000,"/");

setcookie("pass_cookie", "", time()-50000,"/");

}

if(isset($_SESSION['username'])){ 
echo("we could not log out try again!");
exit();
}else{
 header("Location: home.php");

}

?>
Jon
  • 4,746
  • 2
  • 24
  • 37
dev leb
  • 65
  • 1
  • 1
  • 8

3 Answers3

0

Not sure of your error, but reading about session_destroy would help you out.

My guess is the error is when checking if(isset($_SESSION['username'])){, as, according to the manual:

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. To use the session variables again, session_start() has to be called.

You never start the session again, so you can't use session variables.

Also, for further assistance with logging a user out, the following from the same page will help:

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.

And while it's not in the scope of the question - it will help you make a successful logout script.

Jon
  • 4,746
  • 2
  • 24
  • 37
  • sir i need another help from you if it is possible i have an error with profile.php code [http://stackoverflow.com/questions/15046785/user-profile-data-does-not-appear-after-logging-in] can you take a look and maybe help me to solve the issue – dev leb Feb 24 '13 at 01:21
  • @user2097286 I'll take a loot at it. Did this answer help you out? – Jon Feb 24 '13 at 01:23
  • a little because i am new to the php and i do not know about the sessions and cookies so iam trying to learn it and get the help anw thank you for your reply and your help – dev leb Feb 24 '13 at 01:26
0

session_destroy(); destroys all the sessions, so im not following the point of your If statement to check for username session ??

Secondly you don't actually need brackets for echo:

echo("we could not log out try again!"); exit();

Can be written as:

echo "we could not log out try again!"; exit; 
Sir
  • 8,135
  • 17
  • 83
  • 146
  • you mean i do not need the if statment because the echo from this if appear and do not let me logout that make me crazy – dev leb Feb 24 '13 at 01:29
  • 1
    You dont need `if(isset($_SESSION['username'])){ ` you destroyed the session it won't be set so its a waste to check. – Sir Feb 24 '13 at 01:32
0
<?php
session_start();
session_destroy();
if(isset($_COOKIE['id_cookie'])){
  setcookie("id_cookie", "", time()-50000,"/");
  setcookie("pass_cookie", "", time()-50000,"/");
}

header("Location: home.php");
exit();
?>
richard
  • 1,456
  • 4
  • 15
  • 22