thinkphpեȡÓû§Öظ´µÇ¼
Ëæ×Å»¥ÁªÍøµÄÒ»Ö±Éú³¤£¬Ô½À´Ô½¶àµÄÍøÕ¾ºÍÓ¦ÓóÌÐòÒªÇóÓû§¾ÙÐÐ×¢²áºÍµÇ¼£¬ÒÔ±ãÌṩԽ·¢¸öÐÔ»¯ºÍÇå¾²µÄЧÀÍ¡£¿ÉÊÇ£¬±£´æÒ»¸öÎÊÌ⣬ÓÐЩÓû§»áͬʱÔÚ¶à¸ö×°±¸»òä¯ÀÀÆ÷ÖеǼͳһ¸öÕʺţ¬Õâ¿ÉÄܻᵼÖÂÊý¾ÝÇå¾²·½ÃæµÄÎÊÌ⣬ÀýÈçÐÅÏ¢×ß©£¬»òÕß·ºÆðÊý¾Ý³åÍ»µÈÎÊÌâ¡£
Òò´Ë£¬ÔÚÏÖʵµÄÓ¦Óó¡¾°ÖУ¬ÎÒÃÇÐèÒª½â¾öÕâ¸öÎÊÌ⣬¼´Õ¥È¡Í³Ò»¸öÓû§ÔÚ¶à¸ö×°±¸»òä¯ÀÀÆ÷ÖÐͬʱµÇ¼ͳһ¸öÕʺš£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃThinkPHP¿ò¼ÜÀ´ÊµÏִ˹¦Ð§¡£
Ê×ÏÈ£¬ÎÒÃÇÐèҪȷ±£Óû§ÔڵǼʱÌìÉúÒ»¸öΨһµÄÉí·Ý±êʶ·û£¬Õâ¸ö±êʶ·û¿ÉÒÔÓÃÊý¾Ý¿âÖеÄÖ÷¼ü»òÕßÊÇËæ»úÌìÉúµÄÒ»¸ö×Ö·û´®×÷ΪÉí·Ý±êʶ·û¡£ÔÚ¸ÃÓû§¾ÙÐеǼʱ£¬ÎÒÃÇÐèÒª½«¸Ã±êʶ·û´æ´¢ÔÚSession»òÕßCookieÖУ¬Àû±ãºóÐøÑé֤ijһ¸öÓû§ÊÇ·ñÒѾµÇ¼¡£
µ±Óû§µÇ¼ʱ£¬ÎÒÃÇÐèÒª´ÓÊý¾Ý¿âÖÐÅÌÎʸÃÓû§ÊÇ·ñÒѾ±£´æÓÐÓõĵǼ±êʶ·û£¬ÈôÊDZ£´æ£¬Ôò˵Ã÷¸ÃÓû§ÒѾÔÚÆäËû×°±¸»òä¯ÀÀÆ÷ÖеǼÁ˸ÃÕʺţ¬´ËʱÐèÒªÌáÐÑÓû§Í˳öÆäËûµÄµÇ¼»á»°£¬²¢ÖØеǼ¡£
´úÂëʾÀýÈçÏ£º
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
/** * µÇ¼ÑéÖ¤ */ public function login(){ $username = I('post.username'); $password = I('post.password'); $user = M('User')->where(array('username'=>$username))->find(); if (!$user) { $this->error('Óû§²»±£´æ£¡'); }elseif(md5($password.$user['salt']) !== $user['password']){ $this->error('ÃÜÂë¹ýʧ£¡'); }else{ // ÅжÏÓû§ÊÇ·ñÒѾµÇ¼ $uid = $user['id']; // »ñÈ¡Óû§ID $session_uid = session('uid'); // ´ÓSessionÖлñÈ¡Óû§ID $session_sid = session('sid'); // ´ÓSessionÖлñÈ¡µÇ¼±êʶ·û if($uid == $session_uid && $session_sid){ // ÅжÏÓû§ÊÇ·ñÒѾµÇ¼ $this->error('ÄúÒѾÔÚÆäËûÉè±¹ØÁ¬Ç¼£¬ÇëÏÈÍ˳öÆäËûµÄµÇ¼»á»°£¡'); }else{ // ÌìÉúеÄÉí·Ý±êʶ·û $sid = md5(uniqid(mt_rand(), true)); // ÌìÉúËæ»ú×Ö·û´®×÷ΪÉí·Ý±êʶ·û session('uid', $uid); // ½«Óû§ID´æ´¢µ½SessionÖÐ session('sid', $sid); // ½«µÇ¼±êʶ·û´æ´¢µ½SessionÖÐ $this->success('µÇ¼Àֳɣ¡'); } } }
µÇ¼ºó¸´ÖÆ
ÔÚÒÔÉÏ´úÂëÖУ¬ÎÒÃÇÊ×ÏÈ´ÓÊý¾Ý¿âÖÐÅÌÎʸÃÓû§µÄÐÅÏ¢£¬È»ºóÑéÖ¤Óû§µÄÕʺźÍÃÜÂëÊÇ·ñ׼ȷ¡£ÈôÊÇÑé֤ͨ¹ý£¬ÄÇô¾ÍÅжÏÓû§ÊÇ·ñÒѾÔÚÆäËû×°±¸»òä¯ÀÀÆ÷ÖеǼÁ˸ÃÕʺţ¬ÈôÊÇÓУ¬ÔòÌáÐÑÓû§Í˳öÆäËûµÄµÇ¼»á»°¡£
ÈôÊÇÓû§Ã»ÓÐÔÚÆäËû×°±¸»òä¯ÀÀÆ÷ÖеǼ¸ÃÕʺţ¬ÄÇô¾ÍÌìÉúÒ»¸öеÄÉí·Ý±êʶ·û£¬²¢½«Óû§IDºÍµÇ¼±êʶ·û´æ´¢µ½SessionÖС£ÕâÑù£¬Ï´ÎÓû§¾ÙÐвÙ×÷ʱ£¬ÎÒÃǾͿÉÒÔÑéÖ¤Óû§µÄÉí·ÝÊÇ·ñ׼ȷ¡£
ÔÚ´úÂëʵÏÖµÄÀú³ÌÖУ¬ÎÒÃÇʹÓÃÁËSessionÀ´´æ´¢Óû§µÄµÇ¼ÐÅÏ¢£¬ÕâÑù×öÓÐÒ»¸öÎÊÌ⣬¼´µ±Óû§¹Ø±Õä¯ÀÀÆ÷ʱ£¬SessionÖд洢µÄÐÅÏ¢»á±»É¾³ý£¬´ËʱÓû§ÐèÒªÖØоÙÐеǼ²Ù×÷¡£Òò´Ë£¬ÔÚÏÖʵµÄÓ¦ÓÃÖУ¬ÎÒÃÇ¿ÉÒÔ½«SessionÖеÄÐÅÏ¢´æ´¢µ½Êý¾Ý¿âÖлòÕßʹÓÃRedisµÈ»º´æ¹¤¾ß¾ÙÐÐÖÎÀí£¬ÕâÑù¿ÉÒÔÓÐÓõؽâ¾öSessionÓâÆÚµÄÎÊÌâ¡£
×ܽ᣺
±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃThinkPHP¿ò¼ÜÀ´Õ¥È¡Í³Ò»¸öÓû§ÔÚ¶à¸ö×°±¸»òä¯ÀÀÆ÷ÖÐͬʱµÇ¼ͳһ¸öÕʺŵÄÒªÁ졣ͨ¹ýÑéÖ¤Óû§µÇ¼ʱµÄÉí·Ý±êʶ·û£¬ÎÒÃÇ¿ÉÒÔÓÐÓõرÜÃâÊý¾ÝÇå¾²·½ÃæµÄÎÊÌâ¡£ÔÚÏÖʵӦÓÃÖУ¬ÎÒÃÇ»¹¿ÉÒÔ¶ÔSessionµÄÖÎÀí¾ÙÐÐÓÅ»¯£¬ÒÔÌá¸ßÓ¦ÓõÄÐÔÄܺÍÎȹÌÐÔ¡£
ÒÔÉϾÍÊÇthinkphpեȡÓû§Öظ´µÇ¼µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡