ÔõÑùÔÚThinkPHP6ÖÐʹÓÃSession¾ÙÐлỰ²Ù×÷£¿
Ëæ×ÅwebÓ¦ÓóÌÐòµÄÈÕÒæÆÕ¼°£¬»á»°ÖÎÀí±äµÃÔ½À´Ô½Ö÷Òª¡£ÔÚwebÓ¦ÓóÌÐòÖУ¬»á»°¾³£ÓÃÓÚ¸ú×ÙÓû§Ô˶¯£¬´æ´¢Óû§Êý¾ÝÒÔ¼°Î¬»¤Óû§×´Ì¬¡£ÔÚphpÖУ¬»á»°ÖÎÀíͨ³£Ê¹ÓÃsessionÀ´ÊµÏÖ¡£thinkphp6ÌṩÁËÖÜÈ«µÄ»á»°Ö§³Ö£¬¿ÉÒÔÇáËɵØÓësession½»»¥¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚthinkphp6ÖÐʹÓÃsession¾ÙÐлỰ²Ù×÷¡£
¿ªÆôSessionÖ§³Ö
ÔÚThinkPHP6ÖУ¬¿ÉÒÔͨ¹ýÉèÖÃÎļþÀ´¿ªÆôSessionÖ§³Ö¡£ÔÚconfigĿ¼ÏµÄapp.phpÎļþÖУ¬¿ÉÒÔÕÒµ½ÒÔÏÂÉèÖÃÏ
// sessionÉèÖÃ 'session' => [ 'prefix' => 'think', 'type' => '', 'auto_start' => true, 'httponly' => true, 'secure' => false, 'expire' => 1440, 'use_trans_sid' => true, 'cookie_domain' => '', 'cookie_path' => '/', 'cookie_lifetime' => 0, 'cookie' => null, ],
µÇ¼ºó¸´ÖÆ
ͨ¹ýÐ޸ĸÃÉèÖÃÏ¿ÉÒÔÇáËɵؿªÆôSessionÖ§³Ö¡£ÆäÖУ¬prefixÊÇSession±äÁ¿Ç°×º£¬typeÊÇSession´æ´¢ÀàÐÍ£¨¿ÉÒÔÊÇfile¡¢redis¡¢memcachedµÈ£©£¬auto_startÊÇÊÇ·ñ×Ô¶¯¿ªÆôSession£¬httponlyÊÇÊÇ·ñÖ»ÄÜͨ¹ýHTTPÐÒé»á¼ûCookie£¬secureÊÇÊÇ·ñʹÓÃÇå¾²HTTPÐÒ飬expireÊÇSessionÓâÆÚʱ¼ä£¨µ¥Î»ÎªÃ룩£¬use_trans_sidÊÇÊÇ·ñÆôÓÃ͸Ã÷SessionID£¬cookie_domainÊÇSessionµÄCookieÓòÃû£¬cookie_pathÊÇSessionµÄCookie·¾¶£¬cookie_lifetimeÊÇSessionµÄCookieÓâÆÚʱ¼ä£¨µ¥Î»ÎªÃ룩£¬cookieÊÇÊÖ¶¯ÉèÖÃSessionµÄCookieÑ¡Ïî¡£
×îÏȻỰ
ÔÚ¿ªÆôSessionÖ§³ÖÖ®ºó£¬¿ÉÒÔʹÓÃSessionÀàÀ´×îÏȻỰ¡£ÔÚ¿ØÖÆÆ÷»òÄ£×ÓÖУ¬Í¨¹ýŲÓÃSessionÀàµÄstartÒªÁìÀ´×îÏȻỰ£¬ÈçÏÂËùʾ£º
use thinkacadeSession; ... // ×îÏȻỰ Session::start();
µÇ¼ºó¸´ÖÆ
ÉèÖÃSession±äÁ¿
ÔڻỰʱ´ú£¬¿ÉÒÔͨ¹ýSessionÀàµÄsetÒªÁìÀ´ÉèÖÃSession±äÁ¿¡£ÕâЩ±äÁ¿½«»áÔÚÕû¸ö»á»°Ê±´ú¼á³ÖÎȹ̣¬Ö±µ½»á»°¿¢Ê»òÕß±»ÊÖ¶¯É¾³ý¡£Ê¹ÓÃÒªÁìÈçÏ£º
use thinkacadeSession; ... // ÉèÖÃSession±äÁ¿ Session::set('key', 'value');
µÇ¼ºó¸´ÖÆ
¿ÉÒÔʹÓÃSessionÀàµÄgetÒªÁìÀ´»ñÈ¡Session±äÁ¿µÄÖµ£¬ÈçÏÂËùʾ£º
use thinkacadeSession; ... // »ñÈ¡Session±äÁ¿ $value = Session::get('key');
µÇ¼ºó¸´ÖÆ
ɾ³ýSession±äÁ¿
ͨ¹ýŲÓÃSessionÀàµÄdeleteÒªÁ죬¿ÉÒÔɾ³ýSession±äÁ¿¡£Ê¹ÓÃÒªÁìÈçÏ£º
use thinkacadeSession; ... // ɾ³ýSession±äÁ¿ Session::delete('key');
µÇ¼ºó¸´ÖÆ
Çå¿ÕSession
ÈôÊÇÐèÒªÔڻỰʱ´úɨ³ýËùÓÐSession±äÁ¿£¬¿ÉÒÔʹÓÃSessionÀàµÄclearÒªÁì¡£ÈçÏÂËùʾ£º
use thinkacadeSession; ... // Çå¿ÕSession Session::clear();
µÇ¼ºó¸´ÖÆ
ÑéÖ¤Session
ÔڻỰʱ´ú£¬¿ÉÒÔʹÓÃSessionÀàµÄhasÒªÁìÀ´ÑéÖ¤Session±äÁ¿ÊÇ·ñ±£´æ¡£ÈçÏÂËùʾ£º
use thinkacadeSession; ... // ÑéÖ¤Session±äÁ¿ÊÇ·ñ±£´æ if (Session::has('key')) { // ±£´æ } else { // ²»±£´æ }
µÇ¼ºó¸´ÖÆ
ÉÁ´æÊý¾Ý
ÉÁ´æÊý¾ÝÊÇÖ¸Ö»ÄÜÔÚÏÂÒ»´Î»á»°Ê±´úʹÓõÄÑÓ³ÙÉèÖÃSession±äÁ¿¡£¿ÉÒÔͨ¹ýSessionÀàµÄflashÒªÁìÀ´ÉèÖÃÉÁ´æÊý¾Ý¡£Ê¹ÓÃÒªÁìÈçÏ£º
use thinkacadeSession; ... // ÉèÖÃÉÁ´æÊý¾Ý Session::flash('key', 'value');
µÇ¼ºó¸´ÖÆ
ÔÚÏÂÒ»´ÎÇëÇóʱ£¬¿ÉÒÔʹÓÃSessionÀàµÄgetÒªÁìÀ´»ñÈ¡ÉÁ´æÊý¾Ý¡£ÈçÏÂËùʾ£º
use thinkacadeSession; ... // »ñÈ¡ÉÁ´æÊý¾Ý $value = Session::get('key');
µÇ¼ºó¸´ÖÆ
¿¢Ê»Ự
ÔڻỰ¿¢ÊÂʱ£¬¿ÉÒÔͨ¹ýŲÓÃSessionÀàµÄdestroyÒªÁìÀ´É¨³ýËùÓлỰÊý¾Ý£¬²¢ÊÍ·ÅËùÓлỰ×ÊÔ´¡£Ê¹ÓÃÒªÁìÈçÏ£º
use thinkacadeSession; ... // ¿¢Ê»Ự Session::destroy();
µÇ¼ºó¸´ÖÆ
±¾ÎÄÏÈÈÝÁËÔõÑùÔÚThinkPHP6ÖÐʹÓÃSession¾ÙÐлỰ²Ù×÷¡£ÕâÈÝÒ×Ã÷È·²¢ÇÒÒ×ÓÚʹÓ㬿ÉÓÃÓÚ¸ú×ÙÓû§Ô˶¯£¬´æ´¢Óû§Êý¾ÝÒÔ¼°Î¬»¤Óû§×´Ì¬¡£ÈôÊÇÄúÔÚʹÓÃÖÐÓöµ½ÎÊÌâ»òÕßÓÐÆäËûÒÉÎÊ£¬¿ÉÒԲο¼¹Ù·½Îĵµ»òÕßÔÚÉçÇøÖоÙÐÐÌáÎÊ£¬ÒÔ±ã»ñµÃ¸üºÃµÄ×ÊÖú¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÒÔÉϾÍÊÇÔõÑùÔÚThinkPHP6ÖÐʹÓÃSession¾ÙÐлỰ²Ù×÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡