How to destroy two different sessions in the same PHP script?

At the beginning of the script session, I select the name session_name and then execute session_start.

$session_name = rand(0,1) ? 'first' : 'second'; session_name($session_name); session_start(); 

The above code works on every page load. After several page loads, there will likely be sessions for both session_name 'first' and session_name 'second'.

This is my code that is in my logout script to try to destroy both of these sessions:

 session_start(); session_name("first"); session_unset(); session_destroy(); session_write_close(); session_start(); session_name("second"); session_unset(); session_destroy(); session_write_close(); 

The script above destroys first, but not the second. If I run log out script a second time, then it will destroy the second session, since the first has already been deleted.

How to destroy sessions for both session names in the same script?

+5
source share
2 answers

TRY IT

 <?php session_name("first"); session_start(); echo session_status(); session_destroy(); echo session_status(); session_name("second"); session_start(); echo session_status(); session_destroy(); echo session_status(); ?> 

I tested it on xampp and it returns 2121, which means that the session is active, does not exist, the session is active, does not exist.

I placed session_name () before session_start (), because setting the name, like you, did not take effect after the session is already running. I was looking for it, and it should do something with the php.ini file, where session.auto_start is set to "true".

An explanation and discussion on this topic can be found here: http://php.net/manual/en/function.session-name.php - check comments.

Edition:

After re-viewing your edit, you basically do not create two sessions, but only one, and it starts with the random name "first" or "second". You can destroy the session, but the cookie will remain. I checked your xampp code and it does just that. First, php starts one session and saves a cookie with a temporary value of β€œsession”, after reloading the page it will load one of the two parameters again, but this time it will change the existing cookie time to β€œN / A”. You should find a solution to clear cookies in your domain after loading the page as follows:

 <?php $session_name = rand(0,1) ? 'first' : 'second'; session_name($session_name); session_start(); deleteCookies($session_name); function deleteCookies($skip_this_one) { if (isset($_SERVER['HTTP_COOKIE'])) { $cookies = explode(';', $_SERVER['HTTP_COOKIE']); foreach($cookies as $cookie) { $parts = explode('=', $cookie); $name = trim($parts[0]); if ($name == $skip_this_one) { //skip }else{ setcookie($name, '', time()-1000); setcookie($name, '', time()-1000, '/'); } } } //do something inside your session //when done, stop the session the way you want session_destroy(); } ?> 

This will delete all previous cookies and save the current session.

This solution is rewritten here: how to delete all cookies of my website in php

Hope this helps :)

+2
source

Please try below code

 <?php unset($_SESSION['name']); // will delete just the name data session_destroy(); // will delete ALL data associated with that user. ?> 
-1
source

Source: https://habr.com/ru/post/1238297/


All Articles