Advertisement
Guest User

Untitled

a guest
Oct 19th, 2011
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.56 KB | None | 0 0
  1. <?php
  2. ## Class cSession()
  3. ##
  4. ## Written by Henric Johansson, henric-johansson@hotmail.com 2011-10-19
  5. ## Handles your $_SESSION variables
  6.  
  7. class cSession {
  8.     private $sessionStarted = false;
  9.  
  10.     function __construct() {
  11.         ## Our constructor, initialize session here
  12.         if(!$this->sessionStarted) {
  13.  
  14.             ## Start the session and set the variable
  15.             session_start();
  16.             $this->sessionStarted = true;
  17.         }
  18.     }
  19.  
  20.     ## Function get($key)
  21.     ##
  22.     ## Returns the value of $key, if variable was not set, returns false
  23.  
  24.     function get($key) {
  25.         ## If we the key is not set, return nothing
  26.         if(!isset($_SESSION[$key])) {
  27.             return false;
  28.         } else {
  29.             ## If it is set, return the value.
  30.             return $_SESSION[$key];
  31.         }
  32.     }
  33.  
  34.     ## Function set($value, $key)
  35.     ##
  36.     ## Sets a key $key to the value, $value
  37.  
  38.     function set($value, $key) {
  39.         $_SESSION[$key] = $value;
  40.     }
  41.  
  42.     ## Function debugSession()
  43.     ##
  44.     ## Prints the session variable in a prepared html tag if no session was set, echo an error message
  45.  
  46.     function debugSession() {
  47.         if($this->sessionStarted) {
  48.             echo '<pre>';
  49.             print_r($_SESSION);
  50.             echo '</pre>';
  51.         } else {
  52.             echo '<pre>';
  53.             echo '$_SESSION not initialized';
  54.             echo '</pre>';
  55.         }
  56.     }
  57.  
  58.     ## Function kill()
  59.     ##
  60.     ## Returns true if session was killed, returns false if there was no session
  61.  
  62.     function kill() {
  63.         if($this->sessionStarted) {
  64.             ## If session was started, destroy and unset it.
  65.             session_unset();
  66.             session_destroy();
  67.  
  68.             return true;
  69.         } else {
  70.  
  71.             ## If there was no session, return false
  72.             return false;
  73.         }
  74.     }
  75. }
  76. ?>
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement