php简单的会话类代码
程序员文章站
2022-09-24 17:10:49
复制代码 代码如下:
<?php
class session
{
static $sessionobject;
/* $_session['user'] == 0 , don't work .
* == 1 , he is really user .
*/
private function __construct()
{
if(! ( isset($_session['user']) && $_session['user']==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['user'] = 0 ; // for php>=5
}
public function set_true()
{
if( isset($_session['user']) )
$_session['user'] = 1 ;
else
$this->error();
}
public function get_status()
{
return $_session['user'];
}
public function end_session()
{
session_destroy();
}
public function error()
{
}
}
?>
大概用法
example :
log in page : $session = session::getobject();
$session->set_ture(); // if $row['password'] ==
next page :
$session = session::getobject();
if($session->get_status())
// .... the user is really
else
// ....
log out : $session = session::getobject();
$session->end_session();
由get_status()返回当前用户是否有效登录
复制代码 代码如下:
<?php
class session
{
static $sessionobject;
/* $_session['user'] == 0 , don't work .
* == 1 , he is really user .
*/
private function __construct()
{
if(! ( isset($_session['user']) && $_session['user']==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['user'] = 0 ; // for php>=5
}
public function set_true()
{
if( isset($_session['user']) )
$_session['user'] = 1 ;
else
$this->error();
}
public function get_status()
{
return $_session['user'];
}
public function end_session()
{
session_destroy();
}
public function error()
{
}
}
?>
大概用法
example :
log in page : $session = session::getobject();
$session->set_ture(); // if $row['password'] ==
next page :
复制代码 代码如下:
$session = session::getobject();
if($session->get_status())
// .... the user is really
else
// ....
log out : $session = session::getobject();
$session->end_session();
由get_status()返回当前用户是否有效登录