nathan15

CodeIgniter Custom Library for Native PHP Sessions

Jun 19th, 2014
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.86 KB | None | 0 0
  1. <?php
  2.  
  3. if(!defined('BASEPATH')) exit( 'No direct script access allowed' );
  4.  
  5. class PHP_session {
  6.  
  7.     public function __construct()
  8.     {
  9.         $this->start();
  10.     }
  11.  
  12.     public function start()
  13.     {
  14.         $session_name = 'session'; // Set a custom session name
  15.         $secure = false; // Set to true if using https.
  16.         $httponly = true; // This stops javascript being able to access the session id.
  17.         ini_set('session.use_only_cookies', 1); // Forces sessions to only use cookies.
  18.         $cookieParams = session_get_cookie_params(); // Gets current cookies params.
  19.         session_set_cookie_params($cookieParams["lifetime"], $cookieParams["path"], $cookieParams["domain"], $secure, $httponly);
  20.         session_name($session_name); // Sets the session name to the one set above.
  21.  
  22.         if(!isset($_SESSION)) session_start();
  23.     }
  24.  
  25.     public function destroy()
  26.     {
  27.         session_destroy();
  28.     }
  29.  
  30.     public function set($key, $value = null)
  31.     {
  32.         if(is_array($key)) {
  33.             foreach($key as $k => $v) {
  34.                 $_SESSION[$k] = $v;
  35.             }
  36.         }
  37.         else {
  38.             $_SESSION[$key] = $value;
  39.         }
  40.     }
  41.  
  42.     public function get($key)
  43.     {
  44.         return isset($_SESSION[$key]) ? $_SESSION[$key] : null;
  45.     }
  46.  
  47.     public function regenerate_id($del_old = false)
  48.     {
  49.         session_regenerate_id($del_old);
  50.     }
  51.  
  52.     public function delete($key)
  53.     {
  54.         unset($_SESSION[$key]);
  55.     }
  56.  
  57.     public function flashdata($key)
  58.     {
  59.         $key = 'flashdata_'.$key;
  60.         $value = $this->get($key);
  61.         $this->delete($key);
  62.         return $value;
  63.     }
  64.  
  65.     public function set_flashdata($key, $value)
  66.     {
  67.         $this->set('flashdata_'.$key, $value);
  68.     }
  69.  
  70. }
  71.  
  72. /* End of file php_session.php */
  73. /* Location: ./application/libraries/php_session.php */
Advertisement
Add Comment
Please, Sign In to add comment