Home  >  Article  >  Backend Development  >  PHP determines whether the user has logged in instance analysis

PHP determines whether the user has logged in instance analysis

墨辰丷
墨辰丷Original
2018-06-01 10:48:503664browse

This article mainly introduces the relevant information on PHP to determine whether the user has logged in. It is very good and has reference value. Friends in need can refer to it

1.

2.

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

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

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

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Several points to note when defining command space in PHP

PHP request remote address setting timeout Solution

phpSimple example of importing Excel table into MySQL database through PHPExcel

The above is the detailed content of PHP determines whether the user has logged in instance analysis. For more information, please follow other related articles on the PHP Chinese website!

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