PHP虽然是开源的 不过有些东西确实做的不好,比如说SESSION就很麻烦了,要COOKIES启用才可以,是保存在COOKIES中,如果做象没有COOKIES的手机网站(很多手机浏览器没有COOKIES),就很麻烦了,以下是我做的一种方法在COOKIES不起用的情况下判断用户是否登陆,其中SessionId是保存在服务器的数据库上的。
//是否登陆
function islogin($uid)
{
//查询sessionid
$this->dataconnect();
$sqlsid="select * from wap_usersession where user_id=$uid ORDER BY last_visit DESC LIMIT 0,1";
$sr=$this->db->Execute($sqlsid);
if(!$sr)
{
}
else
{
while($sa=$sr->FetchRow())
{
$sid=$sa['session_id'];
$stime=$sa['last_visit'];
}
}
//要检验session是否超时的页面:
$timeout=3600; //超时时间,单位:秒,这里设为1小时.
$now=time();
if(isset($sid))
{
session_id($sid);
//启用session
session_start();
if(isset($_SESSION[ "session_time "]))
{
}
else
{
$_SESSION[ "session_time "]=time();
}
if(($now-$stime) >$timeout&&($now-$_SESSION[ "session_time "]) >$timeout)
{
//超时了.
unset($_SESSION['username']);
unset($_SESSION['session_time ']);
session_destroy();
$sqldelete="DELETE FROM user_session WHERE user_id=$uid";
$deletresult=$this->db->Execute($sqldelete);
}
else
{
//还没超时.
$_SESSION[ "session_time "]=time();
}
}
if(isset($_SESSION['username']))
{
return true;
}
else
{
return false;
}
}
//数据库连接
function dataconnect()
{
include_once (dirname(__FILE__)."/db/adodb.inc.php");
$this->db = ADONewConnection();
$this->db->createdatabase = true;
$result = $this->db->Connect( DB_HOST,DB_USER,DB_PASSWORD,DB_DATABASE );
if(!$result)
{
die("Could not connect to the database.");
}else{
$this->db->Execute("set names 'utf8'");
return $this->db;
}
}