Home >php教程 >php手册 >Determine whether the user is logged in (jump to different pages or perform different actions)

Determine whether the user is logged in (jump to different pages or perform different actions)

WBOY
WBOYOriginal
2016-09-23 03:30:101397browse

1.

2.

   <span style="color: #0000ff;"><</span><span style="color: #800000;">if </span><span style="color: #ff0000;">condition</span><span style="color: #0000ff;">="$GLOBALS['userinfo']['user_id'] gt 0"</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"><</span><span style="color: #800000;">span </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="help_wz"</span><span style="color: #0000ff;">><</span><span style="color: #800000;">a </span><span style="color: #ff0000;">href</span><span style="color: #0000ff;">="{$Think.config.VIP_URL}/Member/user_pwd/"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="color:#404958;"</span><span style="color: #0000ff;">></span>找回登录密码<span style="color: #0000ff;"></</span><span style="color: #800000;">a</span><span style="color: #0000ff;">></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"><</span><span style="color: #800000;">else</span><span style="color: #0000ff;">/></span>
        <span style="color: #0000ff;"><</span><span style="color: #800000;">span </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="help_wz"</span><span style="color: #0000ff;">><</span><span style="color: #800000;">a </span><span style="color: #ff0000;">href</span><span style="color: #0000ff;">="/Login/FindPwd"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="color:#404958;"</span><span style="color: #0000ff;">></span>找回登录密码<span style="color: #0000ff;"></</span><span style="color: #800000;">a</span><span style="color: #0000ff;">></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"></</span><span style="color: #800000;">if</span><span style="color: #0000ff;">></span>

3. Set global variables in the background and query user information

<span style="color: #000000;">    public function UserInfo($user_id){
        //查询系统信息
        $system=array(
                'CmdId'=>'System',
                'Token'=>'wap',
                'PostDetails'=>json_encode(array(
                )));
        //通过curl的post方式发送接口请求
        $curl=new CurlController();
        $strs=$curl->SendCurl($system);
        $GLOBALS['system']=$strs['AcctDetails'];
        if(empty($user_id)){
            return false;
        }
        setcookie("login_uid", $user_id, time() + 3600, "/", C('cookie_url'));
        setcookie("rdun", $this->authcode($user_id . "," . time(), "ENCODE"), time() + 3600,"/",C('cookie_url'));;
        $data=array(
                'CmdId'=>'Individual',
                'Token'=>session('token'),
                'PostDetails'=>json_encode(array(
                        'user_id'=>$user_id,
                )));
        //通过curl的post方式发送接口请求
        $curl=new CurlController();
        $str=$curl->SendCurl($data);
        
        $GLOBALS['userinfo']=$str['AcctDetails'];
        $newtoken=unserialize($str['AcctDetails']['token']);
        $oldtoken=unserialize(session('token'));
        if(!empty(session('token')) && !empty($str['AcctDetails']['token'])){
            //用户不对
            if($newtoken['user_id'] !== $newtoken['user_id']){
                session('token',null);
                session('user_id',null);
                header('Location:/Login/Login');
                return false;
            }
            //token不对
            if($newtoken['time'] !== $oldtoken['time']){
                session('token',null);
                session('user_id',null);
                header('Location:/Login/Login');
                return false;
            }
            //超时的不能在这做,原因是存入session的值不变,如果用户一直操作的话,而且只登陆一次,那么就会出现问题
        }
        return true;
    }</span>

4. Call this method in the background

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn