Home  >  Article  >  Backend Development  >  PHP simple session class code

PHP simple session class code

高洛峰
高洛峰Original
2016-11-30 11:04:451068browse

The code is as follows:

<?php 
class session 
{ 
static $sessionObject; 
/* $_SESSION[&#39;user&#39;] == 0 , don&#39;t work . 
* == 1 , he is really user . 
*/ 
private function __construct() 
{ 
if(! ( isset($_SESSION[&#39;user&#39;]) && $_SESSION[&#39;user&#39;]==0 )) 
session_set_cookie_params(3600); 
session_start(); //Initialize14 }15 private function __clone() 
{ 
} 
public static function getObject() 
{ 
if(! self::$sessionObject instanceof self) 
$sessionObject = new session() ; 
return $sessionObject; 
} 
public function set_false() 
{ 
$_SESSION[&#39;user&#39;] = 0 ; // for PHP>=5 
} 
public function set_true() 
{ 
if( isset($_SESSION[&#39;user&#39;]) ) 
$_SESSION[&#39;user&#39;] = 1 ; 
else 
$this->error(); 
} 
public function get_status() 
{ 
return $_SESSION[&#39;user&#39;]; 
} 
public function end_session() 
{ 
session_destroy(); 
} 
public function error() 
{ 
} 
} 
?>

Approximate usage
example:
log in page: $session = session::getObject();
$session->set_ture(); // if $row['password'] ==
next page:
Copy code The code is as follows:
$session = session::getObject();
if($session->get_status())
// .... the user is really
else
// . ...

log out: $session = session::getObject();
$session->end_session();
Return by get_status() whether the current user is logged in effectively

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn