A
A
Anton2016-08-31 16:10:46
PHP
Anton, 2016-08-31 16:10:46

How to properly implement logout in php?

There is a self-written system. For many years everything worked like clockwork. It worked for so long that we have already forgotten how it all works inside, and doted on. And then suddenly (the reason is unknown) - either the php version was updated, or something was rewritten in the system, and it stopped working .... EXIT.
In Firefox, you have to press exit 3 times for the user to log out. In Chrome, it does not log out at all, even if you close all tabs and restart the browser.
Logout code:

function close_user_session(){

    if ( session_id() ) {
        $q = "DELETE FROM sessions WHERE id_session = '".session_id()."'";
        $del = mysql_query($q);
    }

        setcookie(session_name(), session_id(), time()-60*60*60*24);
    
        // и уничтожаем сессию
        session_destroy();
}

Also tried options (no changes):
function close_user_session(){

        // Если есть активная сессия, удаляем куки сессии,
        setcookie(session_name(), session_id(), time()-60*60*60*24);
        // и уничтожаем сессию
        session_unset();
        session_destroy(); 
}

or stolen from modx evo
setcookie(session_name(), '', 0);
session_destroy();

Please tell me which way to dig to solve the problem.
For debugging, this is what is happening now:
print_r( $_COOKIE);
/* $_COOKIE
 Array
(
    [PHPSESSID] => oec8e0puoh0vvb4j8pkkmiets4
)
*/

/* $_SESSION
Array
(
    [authorized] => 1
    [user_id] => 143
)
*/
    print_r( $_SESSION);


if (isset($_COOKIE[session_name()])) {
    setcookie(session_name(), '', 0);
}
//// now destroy the session
session_destroy();

    print_r( $_COOKIE);
/* $_COOKIE
 Array
(
    [PHPSESSID] => oec8e0puoh0vvb4j8pkkmiets4
)
*/

/* $_SESSION
Array
(
    [authorized] => 1
    [user_id] => 143
)
*/
    print_r( $_SESSION);

As if destroying does not happen
Code:
setcookie(session_name(), session_id(), time()-60*60*60*24);

gives the same result as the listing above.
The destroy function itself outputs:
WARNING - session_destroy(): Session object destruction failed
Строка 187 в файле blablabla\functions.php

By the way, it still depends on the browser:
Mahton - everything works out OK. No jambs.
Firefox - logout occurs after 3-4 clicks on the "exit".
Googlechrome - even if you close the tabs, close the browser, the session is still not broken. Generally. Never. Only clearing the history helps.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton, 2016-09-02
@fattan

Moved session_destroy(); as close to session_start() as possible. Problem solved.

session_start();

if (isset($_GET['logout'])) {
    session_destroy();
    header('location: /bla/bla/bla/index.php?module=auth&action=start' );

}

A
Alexander Sharihin, 2016-08-31
@Pinsky

did not try to go through xdebug and see where it falls off?

B
Boris Yakushev, 2016-08-31
@za4me

Look in git what was rewritten.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question