For logging out a user from my website, I am redirecting the page to logout.php
where I am using session_destroy() function. Even there also, logout functionality is not working without session_start()
function. By adding session_start() function before session_destroy()
function, I am able to logout the user successfully.
Why do I need to use session_start()
function everytime and in every page where I am doing something related to sessions?
session_destroy() destroys the active session. If you do not initialized the session, there will be nothing to be destroyed.
Why do I need to use session_start() function everytime and in every page where I am doing something related to sessions?
So PHP knows which session to destroy. session_start()
looks whether a session cookie or ID is present. Only with that information can you destroy it.
In the default configuration, PHP Sessions operate off of the hard disk. PHP asks you to explicitly tell it when you need this support to avoid unnecessary disk IO.
session_start()
also tells PHP to find out if the user's session exists.
session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.
as per http://php.net/manual/en/function.session-start.php
Essentially by calling session_start()
, PHP reads the header and cross references that session ID to what is on your system(file system/database/etc), which can then populate the $_SESSION
that is relavent to that specific user. Which in turn allows you to call session_destroy()
because it knows what session to actually destroy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With