php中如何判断用户是否登录状态,如何判断用户是否已经登录?

//discuz同服务器下无passport登陆整合方法

//贡献:huozhe3136 QQ:2666556

//假设discuz安装在网站根目录下的bbs/中

//用法事例

error_reporting(E_ALL);

$path_bbs="bbs/";//请修改为你的论坛的相对路径

$check_username="admin";//测试时这个用户名必须在论坛里也存在

$check_password="123456";

define('IN_DISCUZ', TRUE);//为了包含discuz下的文件这个必须定义

require_once "$path_bbs/config.inc.php";//获得论坛数据库的配置

require_once "$path_bbs/include/db_mysql.class.php";

if(@$_GET[act]=="logout"){

foreach ($_COOKIE as $key => $value) {

setcookie($key,'',time() - 3600,$cookiepath,$cookiedomain,0);

header("Location:".$_SERVER['PHP_SELF']);

}

}

if(isset($_POST["submit"])){//检查是否点击了提交按钮

$username=trim($_POST['username']);

$pwd=trim($_POST['password']);

//主站验证登陆,为简单起见,这里只作简单的示范

if($username==$check_username && $pwd==$check_password){//测试时这个用户名必须在论坛里也存在

$db = new dbstuff;

$db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);//$dbhost,$dbuser,$dbpw,$dbname,$pconnect是在bbs/config.inc.php 里定义的变量

$sql="select * from {$tablepre}members where username='$username'";

$rs = $db->query($sql);

if($dzmember = $db->fetch_array($rs)){

setcookie('myusername',$username,time()+3600,$cookiepath,$cookiedomain,0);//设置主站自身的cookie

setcookie("{$tablepre}sid",'',time() - 3600,$cookiepath,$cookiedomain,0);//让discuz的sid过期是为了让discuz重新分配一个新的sid

setcookie("{$tablepre}auth", authcode("$dzmember[password]\t$dzmember[secques]\t$dzmember[uid]", 'ENCODE'),time()+3600,$cookiepath,$cookiedomain,0);//$cookiepath和$ cookiedomainbbs/config.inc.php里定义的变量

header("Location:".$_SERVER['REQUEST_URI']);

}

}

}

//下面这个函数是discuz的用户cookie编码函数,已修改,原型在$path_bbs/include/global.func.php

function authcode($string, $operation) {

global $_SERVER, $_DCACHE,$path_bbs;

require_once "$path_bbs/forumdata/cache/cache_settings.php";//需要获取论坛的authkey

$discuz_auth_key = md5($_DCACHE['settings']['authkey'].$_SERVER['HTTP_USER_AGENT']);

$coded = '';

$keylength = strlen($discuz_auth_key);

$string = $operation == 'DECODE' ? base64_decode($string) : $string;

for($i = 0; $i < strlen($string); $i += $keylength) {

$coded .= substr($string, $i, $keylength) ^ $discuz_auth_key;

}

$coded = $operation == 'ENCODE' ? str_replace('=', '', base64_encode($coded)) : $coded;

return $coded;

}

?>

if(@$_COOKIE['myusername']==''){

?>

用户名:测试用户名为admin,请更改为你的用户名

密码:测试密码为123456

}

else echo("登陆成功,请到论坛检查登陆退出登陆 ");

?>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值