Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remotely destroy a session in php (user logs in somewhere else)?

Tags:

Hey, I'm trying to get my php website to basically "log out" (session_destroy()) when the same user logs in somewhere else. Is there a way to do this? To remotely destroy a specific session?

Thank guys!

Scott

like image 711
twistedpixel Avatar asked Mar 26 '11 15:03

twistedpixel


People also ask

Which method is used to destroy the session 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 do you destroy a cookie session?

If you want to unset all of the values, you can just run a session_destroy() . It will unset all the values and destroy the session. But the $_SESSION array will still work in the same page after session_destroy() . Then you can simply run $_SESSION = array() to reset it's contents.


1 Answers

It's certainly possible, using session_id. When the user logs in somewhere else, you can do this step before starting a new session for the new login:

// The hard part: find out what $old_session_id is  session_id($old_session_id); session_start(); session_destroy();  // Now proceed to create a new session for the new login 

This will destroy the old session on the server side, so when the other computer accesses your application again it will try to access a non-existent session and a new one will be created for it (in which the user is not logged in anymore).

The hard part is finding out what is the ID of the "old" session. There's no one-size-fits-all way of doing that; you need to have some mechanism in place to be able to tell that the session with id XXX belongs to the same user who is logging in now. If you are using database sessions this should be easy enough.

like image 155
Jon Avatar answered Oct 12 '22 23:10

Jon