Does die() end your session in PHP?

后端 未结 2 1524
無奈伤痛
無奈伤痛 2020-12-05 20:30

If I execute:

die();

Will it also effectively call

session_write_close();

Because die stops the entire p

相关标签:
2条回答
  • 2020-12-05 20:51

    No. As you can see on the PHP page: http://www.php.net/manual/en/function.exit.php (because die is equivalent to exit)

    Shutdown functions and object destructors will always be executed even if exit is called.

    BUT, session_write_close is NOT a shutdown function. It will not run if you "die". I suggest you look into session_register_shutdown. This will register session_write_close() as a shutdown function and in this case, it will run on "die".

    More Info: http://www.php.net/manual/en/function.session-register-shutdown.php

    0 讨论(0)
  • 2020-12-05 21:06

    die and exit are equivalent. From the docs:

    Terminates execution of the script. Shutdown functions and object destructors will always be executed even if exit is called.

    So php will still go clean-up routine. Anyway, if you want to do something custom on script shutdown you can register shutdown function using register_shutdown_function.

    function shutdown()
    {
        //Some custom clean-up, logging and so on
    }
    
    register_shutdown_function('shutdown');
    

    Note: if you register your custom shutdown function it will also be called when fatal error occurs.

    0 讨论(0)
提交回复
热议问题