fork download
  1. <?php
  2.  
  3. /**
  4.  * FILE: sessions.php
  5.  *
  6.  * @AUTHOR: LTD shalvasoft
  7.  * @AUTHOR: Shalva Kvaratskhelia
  8.  * PROJECT: MVC
  9.  * VERSION: 1.0.0
  10.  */
  11.  
  12. class sessions{
  13.  
  14. const SESSION_STARTED = TRUE;
  15. const SESSION_NOT_STARTED = FALSE;
  16.  
  17. // The state of the session
  18. private $sessionState = self::SESSION_NOT_STARTED;
  19.  
  20. // THE only instance of the class
  21. private static $instance;
  22.  
  23. private function __construct() {}
  24.  
  25. /**
  26.   * Returns THE instance of 'Session'.
  27.   * The session is automatically initialized if it wasn't.
  28.   *
  29.   * @return object
  30.   **/
  31. public static function getInstance(){
  32. if ( !isset(self::$instance)){
  33. self::$instance = new self;
  34. }
  35. self::$instance->startSession();
  36. return self::$instance;
  37. }
  38.  
  39. /**
  40.   * (Re)starts the session.
  41.   *
  42.   * @return bool TRUE if the session has been initialized, else FALSE.
  43.   **/
  44. public function startSession(){
  45. if ( $this->sessionState == self::SESSION_NOT_STARTED ){
  46. $this->sessionState = session_start();
  47. }
  48. $_SESSION['id'] = session_id();
  49. return $this->sessionState;
  50. }
  51.  
  52.  
  53. /**
  54.   * Stores datas in the session.
  55.   * Example: $instance->foo = 'bar';
  56.   * @param $name
  57.   * @param $value
  58.   */
  59. public function __set( $name , $value ){
  60. $_SESSION[$name] = $value;
  61. }
  62.  
  63. /**
  64.   * Gets datas from the session.
  65.   * Example: echo $instance->foo;
  66.   *
  67.   * @param name Name of the datas to get.
  68.   * @return mixed Datas stored in session.
  69.   **/
  70. public function __get( $name ){
  71. if ( isset($_SESSION[$name])){
  72. return $_SESSION[$name];
  73. }
  74. }
  75.  
  76. public function __isset( $name ){
  77. return isset($_SESSION[$name]);
  78. }
  79.  
  80. public function __unset( $name ){
  81. unset( $_SESSION[$name] );
  82. }
  83.  
  84. /**
  85.   * Destroys the current session.
  86.   *
  87.   * @return bool TRUE is session has been deleted, else FALSE.
  88.   **/
  89. public function destroy(){
  90. if ( $this->sessionState == self::SESSION_STARTED ){
  91. $this->sessionState = !session_destroy();
  92. unset( $_SESSION );
  93. return !$this->sessionState;
  94. }
  95. return FALSE;
  96. }
  97.  
  98. /**
  99.   * Close session
  100.   *
  101.   * @return mixed
  102.   */
  103. public function Close (){
  104. if ( $this->sessionState == self::SESSION_STARTED ){
  105. $this->sessionState = self::SESSION_NOT_STARTED;
  106. }
  107. }
  108.  
  109. /**
  110.   * set session ID
  111.   *
  112.   * @param $sessionid
  113.   *
  114.   * @return mixed
  115.   */
  116. public function SetID ($sessionid){
  117. $file = session_save_path()."/sess_".$sessionid;
  118. if(file_exists($file)){
  119. session_id($sessionid);
  120. }
  121. }
  122.  
  123. /**
  124.   * validate session id
  125.   *
  126.   * @param $session_id
  127.   *
  128.   * @return mixed
  129.   */
  130. public function Validate_id($session_id){
  131. return preg_match('/^[-,a-zA-Z0-9]{1,128}$/', $session_id) > 0;
  132. }
  133.  
  134. /**
  135.   * change session key value by session ID
  136.   *
  137.   * @param $sessionid
  138.   * @param $key
  139.   * @param $val
  140.   *
  141.   * @return mixed
  142.   * @internal param $session_key
  143.   * @internal param $session_val
  144.   *
  145.   */
  146. public function Change ($sessionid, $key, $val){
  147. //Get current session
  148. $current_id = session_id();
  149. $this->Close();
  150. $this->SetID($sessionid);
  151. $this->startSession();
  152. //Set superglobal value
  153. $_SESSION[$key]=$val;
  154. self::Close();
  155. //Set the before session
  156. session_id($current_id);
  157. $this->startSession();
  158. return $this->sessionState;
  159. }
  160.  
  161. /**
  162.   * Reading session by session ID
  163.   *
  164.   * @param $sessionid
  165.   *
  166.   * @param $key
  167.   *
  168.   * @return mixed
  169.   */
  170. public function Read ($sessionid, $key){
  171. //Get current session
  172. $current_id = session_id();
  173. $this->Close();
  174. $this->SetID($sessionid);
  175. $this->startSession();
  176. //Get superglobal value
  177. $value = (isset($_SESSION[$key])) ? $_SESSION[$key]: null;
  178. $this->Close();
  179. //Set the before session
  180. session_id($current_id);
  181. $this->startSession();
  182. return $value;
  183. }
  184. }
  185.  
  186.  
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
Standard output is empty