PHPÖÐThinkPhp¿ò¼ÜµÄtokenÔõôʹÓÃ
Ò»¡¢tokenÔÚThinkPhp¿ò¼ÜµÄʹÓÃ
1. Ê×ÏÈÔÚÊý¾Ý¿âµÄ users ±íÖÐÌí¼ÓÁ½¸ö×Ö¶Îtoken¡¢time_out
token ÓÃÓÚ´æ´¢Óû§µÄ token
time_out ÓÃÓÚÉèÖÃÓû§ token µÄÓâÆÚʱ¼ä
2.½¨É躯Êý
checkToekn($token)
µÇ¼ºó¸´ÖÆ
º¯ÊýÓÃÓÚÄ¥Á· token ÊÇ·ñ±£´æ, ²¢ÇÒ¸üРtoken¡£
public function checkToken($token) { $user = new \app\index\model\Users(); $res = $user->field('time_out')->where('token', $token)->select(); if (!empty($res)) { //dump(time() - $res[0]['time_out']); if (time() - $res[0]['time_out'] > 0) { return 90003; //token³¤Ê±¼äδʹÓöøÓâÆÚ£¬ÐèÖØÐÂÉÏ°¶ } $new_time_out = time() + 604800; //604800ÊÇÆßÌì $res = $user->isUpdate(true) ->where('token', $token) ->update(['time_out' => $new_time_out]); if ($res) { return 90001; //tokenÑéÖ¤Àֳɣ¬time_outË¢ÐÂÀֳɣ¬¿ÉÒÔ»ñÈ¡½Ó¿ÚÐÅÏ¢ } } return 90002; //token¹ýʧÑé֤ʧ°Ü }
µÇ¼ºó¸´ÖÆ
3.½¨É躯Êý
douserLogin($username,$password)
µÇ¼ºó¸´ÖÆ
ÓÃÓÚÑéÖ¤Óû§ÃûÃÜÂë, ²¢ÉÏ°¶, ·µ»Ø token ÐÅÏ¢¡£
public function douserLogin() { $user = new \app\index\model\Users(); $userisset = $user->where('username', $username)->find(); if ($userisset == null) { return json_decode('{"user":"' . $username . '","code":"400","msg":"Óû§²»±£´æ"}'); } else { $userpsisset = $user ->where('username', $username) ->where('password', sha1(md5($password)))->find(); if ($userpsisset == null) { return json_decode('{"user":"' . $username . '","code":"401","msg":"ÃÜÂë¹ýʧ"}'); } else { //session('user', $username); $token = $this->makeToken(); $time_out = strtotime("+7 days"); $userinfo = ['time_out' => $new_time_out, 'token' => $token]; $res = $user->isUpdate(true) ->where('username', $username) ->update($userinfo); if ($res) { return json_decode('{"user":"' . $username . '","toekn":'.$token.' "code":"0","msg":"µÇ¼ÀÖ³É"}'); } } } }
µÇ¼ºó¸´ÖÆ
¶þ¡¢TokenµÄ¿´·¨
tokenÊÇ¿Í»§¶ËƵÈÔÏòЧÀÍÆ÷¶ËÇëÇóÊý¾Ý£¬Ð§ÀÍÆ÷ƵÈÔµÄÈ¥Êý¾Ý¿âÅÌÎÊÓû§ÃûºÍÃÜÂëÅжÏÓû§ÃûºÍÃÜÂë׼ȷÓë·ñ£¬²¢×÷³öÏìÓ¦µÄÌáÐÑ£¬ÔÚÕâÑùµÄÅä¾°Ï£¬token±ãÓ¦Ô˶øÉúÁË¡£
ÒÔÉϾÍÊÇPHPÖÐThinkPhp¿ò¼ÜµÄtokenÔõôʹÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡
ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Á¢³¡£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£