Hello.

I am writing a handler for clicking on the link "Exit"

<? unset($_SESSION[name]); session_destroy(); header('Location: index.php'); ?> 

Swears

Warning: session_destroy() [function.session-destroy]: Trying to destroy uninitialized session in Z:\home\registr\www\test\exit.php on line 3

Warning: Cannot modify header information - headers already sent by (output started at Z:\home\registr\www\test\exit.php:1) in Z:\home\registr\www\test\exit.php on line 4

Why is that?

  • @ Anna, To format the code, select it with the mouse and click on the button 101010 of the editor. - Nicolas Chabanovsky

3 answers 3

At the time of the session_destroy session, your session is inactive and accordingly WARNING is generated. Before calling session_destroy you must add a call to session_start .

 <?php session_start(); unset($_SESSION['name']); // или $_SESSION = array() для очистки всех данных сессии session_destroy(); header('Location: index.php'); 

If you have a redirect right after session_destroy , then it is not necessary to delete the data from the $ _SESSION global variable, the data in it will be available only on the current request.

  • did what you did .. it got this warning: session_start () [function.session-start]: Cannot send session cache limiter - headers already sent (output started at Z: \ home \ registr \ www \ test \ exit.php : 1) in Z: \ home \ registr \ www \ test \ exit.php on line 2 - Anna
  • Just do not forget to exit; after the header: location :) Otherwise, the script will continue to run and the redirect will occur after a full run. At removal $_SESSION['name'] also errors will be. - Sh4dow
  • so how to build a session handler .. ?? - Anna
  • You have an output before calling session_start (Z: \ home \ registr \ www \ test \ exit.php, line 1). - inso
  • session_start () at the beginning of the scripts is .. what output are you talking about? - Anna

The header is triggered because the script has already given the error texts. Headers are only valid until any text is returned to the client, so after throwing errors, headers are not sent (this is your second warning). And on the first - just use session_destroy() , and then shoot down $_SESSION :)

  • that is, session_destroy (); unset ($ _ SESSION [name]); header ('Location: index.php'); just swap places? but the same happened ( - Anna

delete the session and appear before you

 header('Location: index.php'); 

There is some kind of output to the user. echo?

  • session_start (); It starts when a user is authorized, at index.php we return to the head of the page. Only I did not understand what the error was .. - Anna
  • one
    session_start (); It is called not only when a user is authorized it should be called almost the very first each time the page loads for all users, both authorized and not. and apparently it will be much easier to make loguot on Ajax: <script> function logout () {$ .ajax ({url: '/ logout.php'}); } </ script> <a href='#'on onClick='logout();return false;'> log out </ a> or without the Ajax at all: <a href='/logout.php'> log out </ a> and in loguot.php: session_start (); session_destroy (); $ _SESSION = array (); // unset ($ _ SESSION [key]); etc .. header ('location: /index.php'); - Alexander Molofeev