Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is PHP session_destroy() not working?

Tags:

php

session

I tried to destroy all session variable by using the session_destroy() method, but after using this method, the values are not destroyed.

Why is session_destroy() not working?

Is there any other way to destroy the session in PHP?

if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800))  {        session_destroy();        session_unset();      } 
like image 330
Rajasekar Gunasekaran Avatar asked Jun 24 '11 18:06

Rajasekar Gunasekaran


People also ask

What is PHP session_destroy () function?

session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called.

What is PHP session_start () and session_destroy () function?

session_destroy() function: It destroys the whole session rather destroying the variables. When session_start() is called, PHP sets the session cookie in browser. We need to delete the cookies also to completely destroy the session. Example: This example is used to destroying the session.

How do you empty a session variable in PHP?

Destroying a PHP Session A PHP session can be destroyed by session_destroy() function. This function does not need any argument and a single call can destroy all the session variables. If you want to destroy a single session variable then you can use unset() function to unset a session variable.

How can destroy session after some time in PHP?

It can be done by clicking on the logout button or by destroying that session after a fixed time. By default the expiry time of any particular session that is created is 1440 secs i.e. (24*60) i.e. 24 minutes. But in some cases, we need to change the default time accordingly.


1 Answers

Perhaps is way too late to respond but, make sure your session is initialized before destroying it.

session_start() ; session_destroy() ; 

i.e. you cannot destroy a session in logout.php if you initialized your session in index.php. You must start the session in logout.php before destroying it.

like image 86
R.D. Avatar answered Sep 24 '22 20:09

R.D.