Asked  7 Months ago    Answers:  5   Viewed   28 times

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();     
}

 Answers

45

After using session_destroy(), the session is destroyed behind the scenes. For some reason this doesn't affect the values in $_SESSION, which was already populated for this request, but it will be empty in future requests.

You can manually clear $_SESSION if you so desire ($_SESSION = [];).

Wednesday, March 31, 2021
 
francadaval
answered 7 Months ago
82

You need to tell PDO that you want it to throw exceptions:

$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Following your comment below, it is apparent that your DSN is incorrect. It should be:

$connection = new PDO('mysql:host=localhost;dbname=my_db','my_username','xxxxxxx');

Note that the syntax is dbname= rather than dbname: (which you had originally).

Wednesday, March 31, 2021
 
footy
answered 7 Months ago
15

the php-memcached extension supports session locking

http://us3.php.net/memcached

http://us1.php.net/manual/en/memcached.sessions.php

the memcache and memcached extensions look syntactically similar so it may not be too much of a headache to give it a try. (memcached has a stable version 2.1.0 released 2012-08-07).


if you are set on using memcache 2.2.7 you will most likely have to implement the lock yourself by setting some "session_is_locked" variable in your session and then releasing/unsetting it when the script is done writing to the session. Then you'd always need to check if that variable is set before continuing with any scripts which write to the session.

Wednesday, March 31, 2021
 
penpen
answered 7 Months ago
72

There is a problem with Android caching. When you upload app on the device, OS connects your service to Notification Manager. If you ran your app before, Android founds this service in cache so it's not reconnected.

Workaround: before pushing the app on your device, rename your service (refactor option in Android Studio works well) - Android will recognize this service as new one and connect to Notification Manager.

https://developer.android.com/reference/android/service/notification/NotificationListenerService.html#onListenerConnected() - this method will be invoked after connecting to Notification Manager so you can check if your service is connected or not.

Monday, July 19, 2021
 
Silfverstrom
answered 3 Months ago
87

session_destroy() destroys the active session. If you do not initialized the session, there will be nothing to be destroyed.

Thursday, July 29, 2021
 
jab
answered 3 Months ago
jab
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :