Want to delete a PHP Session?

March 15th, 2007 by Quatrux

A lot of PHP novice programmers which don’t want to understand how the sessions work in the beginning, usually doesn’t delete a session the right way, so I wanted to post an example PHP script session_delete(); which deletes everything as needed.

  1. function session_delete($sname = ‘Current User’) {
  2.         # Set Session Name to a Variable
  3.         $name = session_name();
  4.         /* Empty the Cookie from Session */
  5.         if (!headers_sent() ) { setcookie($name,"",0,"/"); }
  6.         /* Remove the Cookie Value */
  7.         unset($_COOKIE[$name]);
  8.         /* Remove all the Info from the Super Global */
  9.         $_SESSION = array();
  10.         /* Free all session variables */
  11.         session_unset();
  12.         /* Destroy all data registered to a session */
  13.         if (session_destroy() === FALSE) {
  14.                 return FALSE;
  15.         } else {
  16.                 return TRUE;
  17.         }       
  18. }

Posted in PHP | No Comments »

Leave a Comment



Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment and don't be afraid of the spam protection!