note 114709 added to function.session-destroy

From: Date: Thu, 27 Mar 2014 10:08:15 +0000
Subject: note 114709 added to function.session-destroy
Groups: php.notes 
Request: Send a blank email to php-notes+get-199052@lists.php.net to get a copy of this message
It took me a while to figure out how to destroy a particular session in php. Note I'm not sure if solution provided below is perfect but it seems work for me. Please feel free to post any easier way to destroy a particular session. Because it's quite useful for functionality of force an user offline. 1. If you're using db or memcached to manage session, you can always delete that session entry directly from db or memcached. 2. Using generic php session methods to delete a particular session(by session id). <?php $session_id_to_destroy = 'nill2if998vhplq9f3pj08vjb1'; // 1. commit session if it's started. if (session_id()) { session_commit(); } // 2. store current session id session_start(); $current_session_id = session_id(); session_commit(); // 3. hijack then destroy session specified. session_id($session_id_to_destroy); session_start(); session_destroy(); session_commit(); // 4. restore current session id. If don't restore it, your current session will refer to the session you just destroyed! session_id($current_session_id); session_start(); session_commit(); ?> ---- Server IP: 72.52.91.14 Probable Submitter: 119.224.37.103 ---- Manual Page -- http://php.net/manual/en/function.session-destroy.php Edit -- https://master.php.net/note/edit/114709 Del: integrated -- https://master.php.net/note/delete/114709/integrated Del: useless -- https://master.php.net/note/delete/114709/useless Del: bad code -- https://master.php.net/note/delete/114709/bad+code Del: spam -- https://master.php.net/note/delete/114709/spam Del: non-english -- https://master.php.net/note/delete/114709/non-english Del: in docs -- https://master.php.net/note/delete/114709/in+docs Del: other reasons-- https://master.php.net/note/delete/114709 Reject -- https://master.php.net/note/reject/114709 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#199052) next »