Good evening everyone!

There is such a problem: A person can log in to the site using an e-mail and password, if the password and e-mail is correct, then a session is created:

session_start(); $_SESSION['email'] = $email; 

There is a button to exit, when you click on it, the following function works:

 function getLogout(){ session_start(); $_SESSION = array(); if (ini_get("session.use_cookies")) { $params = session_get_cookie_params(); setcookie(session_name(), '', time() - 42000, $params["path"], $params["domain"], $params["secure"], $params["httponly"] ); } unset($_SESSION['email']); session_destroy(); } 

Checked through echo $_SESSION['email'] , gives out emptiness after pressing the exit button. Ie session is deleted.

But for some reason, the redirect (code below) does not work, that is, as if the session is still there! Why is that?

 if( !isset($_SESSION['email']) ) { header('http://site.ru'); } 
  • Make sure that there is really nothing there: if (! Isset ($ _ SESSION ['email'])) {header (' site.ru' ); } else {echo "<pre>"; var_dump ($ _ SESSION); } - mrFlyer
  • I was convinced. really nothing. - Alexander Reizan
  • if (! isset ($ _ SESSION ['email'])) {header ('site.ru'); echo '123'; And it gives out 123, but not redirect - Alexander Reizan
  • Well, I set exit, but then just a white screen, I don’t redirect anyway - Alexander Reizan

3 answers 3

It will be right:

 header("Location: http://site.ru"); 
  • Ok, ok. Ladno. Horosho. - MamayAdesu

There is a suspicion that the problem is not in the sessions, but in the encoding of the files. Maybe you got somewhere with a file with BOM.

http://www.larshaendler.com/2015/01/20/remove-bom-with-notepad/

  • I checked the encoding, the file without BOM - Alexander Reizan

Generally solved the problem

Since the site is on wordpress, I used their function

 if(!isset($_SESSION['email'])) { wp_redirect('http://site.ru'); exit(); }