请看效果图
我用的是cookie的方法,请先打开discuz的文件
\source\function\function_member.php
找到函数function setloginstatus()
把以下的代码
dsetcookie('auth', authcode("{$member['password']}\t{$member['uid']}", 'ENCODE'), $cookietime, 1, true);
dsetcookie('loginuser');
变为:
dsetcookie('auth', authcode("{$member['password']}\t{$member['uid']}", 'ENCODE'), $cookietime, 1, true);
dsetcookie('loginuser',$_G['username']);
在首页我们可以这里做:
{if $_COOKIE['fgiC_2132_auth']}
欢迎,{$_COOKIE['fgiC_2132_loginuser']}
|退出
{else}
注册
登录
{/if}
{if $_COOKIE['fgiC_2132_auth']}
就是上面的
dsetcookie('auth', authcode("{$member['password']}\t{$member['uid']}", 'ENCODE'), $cookietime, 1, true);
我们这里判断如果存在这个值就显示出登录的用户名相关的信息,没有的话就显示出登录界面
而
{$_COOKIE['fgiC_2132_loginuser']
代表的是用户名,就是上面的
dsetcookie('loginuser',$_G['username']);
到这里,我们基本就完成了,但是退出怎么做呢?打开文件source\class\class_member.php找到函数function on_logout()系统默认的就是这个用户退出函数,但是请看这个函数中有一个判断语句
if($_GET['formhash'] != $_G['formhash']) {
showmessage('logout_succeed', dreferer(), array('formhash' => FORMHASH, 'ucsynlogout' => $ucsynlogout));
}
而我们一会要做的连接中是没有formhash这个值的,所以我们要新写一个退出函数如下:
//add by jiang in order to logut in the home page
function on_logoff(){
global $_G;
$ucsynlogout = $this->setting['allowsynlogin'] ? uc_user_synlogout() : '';
clearcookies();
$_G['groupid'] = $_G['member']['groupid'] = 7;
$_G['uid'] = $_G['member']['uid'] = 0;
$_G['username'] = $_G['member']['username'] = $_G['member']['password'] = '';
$_G['setting']['styleid'] = $this->setting['styleid'];
showmessage('logout_succeed', dreferer(), array('formhash' => FORMHASH, 'ucsynlogout' => $ucsynlogout));
}
退出
那么到这里是否完成了呢?我们回到首页来点击一下会发现提示没有定义操作logoff解决的方法是:文件source\module\member\member_logging.php找到
if(!in_array($_GET['action'], array('login', 'logout','logoff'))) {
showmessage('undefined_action');
}
在array那里就新增加的logoff,这时再去点就没有提示了
so
done!