- 论坛徽章:
- 0
|
完全面向对象的用户类(OOP)
<?
/*
功能:文件session 来判断用户是否登陆,如果没有登陆,则转向登陆界面.类自动保存当前页面信息
注意: 要求php版本=>;4.2.0,
@author pangzhihui <pang@ptsn.net.cn>;
@copyright no - distributed by www.ptsn.net.cn
@version 1.0 - 2004/10/** update 1.1 - 2004/12/14
update 1.2 -2005/6/13
-----------------------------------------------------
例:
1.不需要判断是否登陆,只是保存页面信息
$USER = new UserLogin;
2.需要判断用户是否登陆,并返回页面信息!
$error = "您还没有登陆,所以不能进行以下操作!";
$USER = new UserLogin;
$USER->;IsLogin($error);
常见问题
Q:浏览器题是错误.Warning: Cannot send session cache limiter - headers already sent (output started at PATH/class.user.inc.php:30)
A:向浏览器传递任何信息之前实现类的实例,否则会发生错误.
Q:session混乱,账号信息发生变化.
A:升级php版本。
例:登陆保存
$USER ->; mData = array("NAME" =>; "",ID=>;"ID" ;
$USER ->; SAVE();
*/
if(!defined(" RO_PATH" )
{
define(" RO_PATH","" ;
}
class UserLogin
{
var $mUser; //为了方便,建议用户一旦登陆,有两个变量存储,用户的登录名,用户主健!
var $mId;
var $mData;
var $mSessionData = "SessionData";
var $mGetVar = array();
var $mCookie = TRUE; //如果session失效,是否利用cookie变量来操作
var $mErrorPage = PRO_PATH ;
/**
* @return UserLogin
* @desc 类的初始化
*/
function UserLogin()
{
//session_cache_limiter('private, must-revalidate');
//2004-12-14修改,解决session失效混乱的问题
//loatval(phpversion())>;4.2
session_cache_limiter('private_no_expire,private, must-revalidate');
session_start();
if(!empty($_SESSION[$this->;mSessionData]))
{
$this->;mData = $_SESSION[$this->;mSessionData];
if(isset($_SESSION[$this->;mSessionData]['NAME']) and isset($_SESSION[$this->;mSessionData]['NAME']))
{
$this->;mUser = $_SESSION[$this->;mSessionData]['NAME'];
$this->;mId = $_SESSION[$this->;mSessionData]['ID'];
}
$this->;mGetVar = $_GET;
}
}
/**
* @return ISLogin
* @desc 判断用户是否登陆
*/
function IsLogin($error="请输入用户名密码!"
{
if(empty($this->;mUser))
{
//echo $error;
$GetVar = "";
if(!empty($this->;mGetVar))
{
foreach($this->;mGetVar as $key=>;$val)
{
$GetVar .= $key."=".$val."&";
}
$_SERVER['PHP_SELF'] .= "?".$GetVar;
}
//header("LOCATION:".$this->;mErrorPage."?&error=$error" ;
//$this->;mErrorPage .= "/login.php";
if(strstr(strtolower($_SERVER["SERVER_SOFTWARE"]) ,"Microsoft-IIS" )
{
$this->;mData["page"] = $_SERVER[" HP_SELF"]."?".$_ENV["QUERY_STRING"];
}
else
{
$this->;mData["page"] = $_SERVER["REQUEST_URI"];
}
$this->;Save();
//header("LOCATION:".$this->;mErrorPage."?page=".$_SERVER['PHP_SELF']."&&error=".urlencode($error)."" ;
echo "<script>; window.alert('".$error."');</script>;";
echo "<meta http-equiv=\"refresh\" content=\"0;url=".$this->;mErrorPage."?page=".$_SERVER['PHP_SELF']."&&error=".urlencode($error)."\">;";
die();
}
else
{
$this->;mUser = $_SESSION[$this->;mSessionData]['NAME'];
$this->;mId = $_SESSION[$this->;mSessionData]['ID'];
}
}
/*
是否存在$this->;mUser
*/
function UserExist()
{
if(empty($this->;mUser))
{
return false;
}
else
{
return true;
}
}
function Save()
{
//保存到session文件
$_SESSION[$this->;mSessionData] = $this->;mData;
//更新cookie变量
$this->;storeSessionCookie();
//$this->;mUser = $_SESSION[$this->;mSessionData]['NAME'];
//$this->;mId = $_SESSION[$this->;mSessionData]['ID'];
}
/**
* @return storeSessionCookie
* @desc 保存变量到cookie
*/
function storeSessionCookie()
{
//print_r($arrayToStore);
$cookValue = "";
//print_r($_SESSION);
foreach( $this->;mData as $key=>;$val )
{
//echo $_SESSION[$this->;mSessionData][$k];
if (isset($_SESSION[$this->;mSessionData][$key]))
{
$cookValue .= base64_encode($key)."-".base64_encode(serialize($val))."|";
}
}
setcookie( $this->;mSessionData , $cookValue, time()+3600*24*365 );
}
function readSessionCookie()
{
$cookie_data = explode( "|", $_COOKIE[$this->;mSessionData] );
if (is_array($cookie_data))
{
foreach ($cookie_data as $val)
{
if(!empty($val))
{
list($sName, $sVar) = explode("-",$val);
$sName = base64_decode($sName);
}
//$sVar = unserialize(base64_decode($sVar));
$_SESSION[$this->;mSessionData][$sName] = unserialize(base64_decode($sVar));
}
}
$this->;mData = $_SESSION[$this->;mSessionData] ;
}
/**
* @return SaveData
* @desc 保存数据倒session文件,同时保存到cookie
*/
function SaveVar($var_name,$var_val)
{
//保存到session文件
$_SESSION[$this->;mSessionData][$var_name] = $var_val;
$this->;mData = $_SESSION[$this->;mSessionData];
//更新cookie变量
$this->;storeSessionCookie($this->;mData);
}
/**
* @return UpdateCookie()
* @desc 更新cookie的值,使cookie和session值相等
*/
function UpdateCookie()
{
$this->;storeSessionCookie($this->;mData);
}
/**
* @return storeSessionCookie
* @desc 删除变量
*/
function DelVar($var_name)
{
unset($_SESSION[$this->;mSessionData][$var_name]);
$this->;mData = $_SESSION[$this->;mSessionData];
//更新cookie变量
$this->;storeSessionCookie($this->;mData);
}
/**
* @return LookCookie()
* @desc 如果session失效,用cookie来代替,并且重新更新session值
*/
function UpdateSession()
{
$this->;readSessionCookie();
}
function Logout()
{
session_destroy();
//print_r($_COOKIE);
if(setcookie ($this->;mSessionData ,"", time()-3600))
return true;
else
return false;
//print_r($_COOKIE);
}
}
?>; |
|