PHP session_start() function: Why I need it everytime I use anything related to PHP sessions

sumit picture sumit · Apr 25, 2011 · Viewed 23.6k times · Source

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?

Answer

Pekka picture Pekka · Apr 25, 2011

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.