ÔõôÐÞ¸ÄThinkPHP6ÖеÄSessionÉèÖÃ
Ò»¡¢SessionÏÈÈÝ
SessionÊÇÒ»ÖÖ´æ´¢Óû§ÐÅÏ¢µÄÊý¾Ý½á¹¹£¬ÔÚWebÓ¦ÓóÌÐòÖÐÉúÑÄÔÚWebЧÀÍÆ÷ÉÏ¡£µ±Óû§ä¯ÀÀÍøվʱ£¬Ð§ÀÍÆ÷»áÌìÉúÒ»¸öSession£¬²¢½«ÆäID·¢Ë͸ø¿Í»§¶Ëä¯ÀÀÆ÷¡£ä¯ÀÀÆ÷ÔÚ·µ»ØÇëÇóʱ£¬»á´øÉϸÃSessionµÄID£¬Ð§ÀÍÆ÷¾Í¿ÉÒÔͨ¹ý¸ÃIDÕÒµ½¸ÃÓû§µÄSession£¬²¢´ÓSessionÖжÁÈ¡»òÐÞ¸ÄÓû§µÄÐÅÏ¢¡£SessionµÄ×÷ÓÃÖ÷Òª¿ÉÒÔ¹éÄÉΪÒÔϼ¸¸ö·½Ã棺
´æ´¢Óû§ÐÅÏ¢¡£ºÃ±ÈÓû§µÄµÇ¼״̬¡¢¹ºÎï³µÄÚÈÝ¡¢±íµ¥Êý¾ÝµÈ¡£
ʵÏÖ¿çÒ³ÃæÊý¾Ý¹²Ïí¡£ºÃ±ÈÔÚ¶à¸öÒ³ÃæÖ®¼ä¹²ÏíÓû§ÐÅÏ¢¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
È·±£Ã¿¸öÓû§¶¼ÓÐ×ÔÁ¦µÄÊý¾Ý´æ´¢¿Õ¼ä¡£
ÔÚThinkPHP6ÖУ¬SessionĬÈÏʹÓÃÁËPHPµÄSession»úÖÆʵÏÖ£¬¿ª·¢Ö°Ô±¿ÉÒÔÔÚÓ¦ÓóÌÐòÖÐÀû±ãµØ²Ù×÷Session¡£
¶þ¡¢SessionÉèÖÃ
SessionµÄÉèÖÃÔÚThinkPHP6ÖÐÖ÷ÒªÉæ¼°ÒÔϼ¸¸ö²ÎÊý£º
session_name£ºSessionÃû³Æ¡£Ä¬ÒÔΪPHPSESSID¡£
expire£ºSessionÓâÆÚʱ¼ä¡£Ä¬ÒÔΪ1440Ãë¡£
use_trans_sid£ºÊÇ·ñÆôÓÃSIDת´ï·½·¨¡£Ä¬ÒÔΪ¹Ø±Õ¡£
auto_start£ºÊÇ·ñ×Ô¶¯Æô¶¯Session¡£Ä¬ÒÔΪ¹Ø±Õ¡£
path£ºSessionÉúÑÄ·¾¶¡£Ä¬ÒÔΪϽµµÍÙʱĿ¼¡£
domain£ºSessionÓòÃû¡£Ä¬ÒÔΪ¿Õ¡£
secure£ºÊÇ·ñÆôÓÃÇå¾²´«Ê䡣ĬÒÔΪ¹Ø±Õ¡£
httponly£ºÊÇ·ñÆôÓÃHttpOnly¡£Ä¬ÒÔΪ¹Ø±Õ¡£
ÈôÊÇÐèÒªÐÞ¸ÄSessionµÄÉèÖã¬ÔÚThinkPHP6ÖпÉÒÔͨ¹ýconfigĿ¼ÏµÄsession.phpÎļþ¾ÙÐÐÉèÖá£Ïêϸ²Ù×÷ÈçÏ£º
½øÈëconfigĿ¼£¬ÕÒµ½session.phpÎļþ¡£
ÐÞ¸Äsession.phpÎļþÖеÄÏà¹ØÉèÖòÎÊý¡£ºÃ±È£º
return [ // session name 'name' => 'think_session', // session auto start 'auto_start' => true, // session type 'type' => '', // expire time 'expire' => 86400, // use database 'use_database' => false, // data compression 'data_compress' => false, // cache prefix 'prefix' => '', // cache expire time 'cache_expire' => null, // session id 'id' => '', // session path 'path' => '', // session domain 'domain' => '', // session secure 'secure' => false, // session httponly 'httponly' => false, // session redis connection 'redis_connection' => 'session_redis', // redis handler 'redis_handler' => '', // redis session key prefix 'redis_prefix' => '', ];
µÇ¼ºó¸´ÖÆ
ÉúÑÄÎļþ²¢¹Ø±Õ¡£
Èý¡¢SessionµÄʹÓÃ
ÔÚThinkPHP6ÖУ¬SessionµÄʹÓúÜÊǼòÆÓ£¬¿ª·¢Ö°Ô±¿ÉÒÔͨ¹ýÒÔÏ·½·¨À´¶ÁÈ¡ºÍдÈëSessionÖеÄÊý¾Ý£º
¶ÁÈ¡SessionÊý¾Ý£º
$request = Request::instance(); $session = $request->session(); $value = $session->get('key');
µÇ¼ºó¸´ÖÆ
дÈëSessionÊý¾Ý£º
$request = Request::instance(); $session = $request->session(); $session->set('key', 'value');
µÇ¼ºó¸´ÖÆ
ÔÚÏÖʵ¿ª·¢ÖУ¬¿ª·¢Ö°Ô±¿ÉÒÔƾ֤ӪҵÐèÇó£¬×Ô½ç˵SessionµÄÏà¹ØÉèÖã¬ÒÔʵÏÖÔ½·¢ÎÞаºÍ¸ßЧµÄÊý¾Ý´æ´¢ºÍÖÎÀí¡£
ÒÔÉϾÍÊÇÔõôÐÞ¸ÄThinkPHP6ÖеÄSessionÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡