欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php简单的会话类代码_PHP教程

程序员文章站 2024-01-19 08:55:28
...
复制代码 代码如下:

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()返回当前用户是否有效登录

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/324193.htmlTechArticle复制代码 代码如下: ?php class session { static $sessionObject; /* $_SESSION['user'] == 0 , don't work . * == 1 , he is really user . */ private function __construct() { if(...