10. Session进阶

龷龳龲龮龬龨龥龣 龟龞龚Drupal8龄龃龀齽龬Session龲齭齫 齪齦齥齡龀齞齞齛齘齕drupal8齏齍齊齉齆齄Symfony齊Session鼼鼸

鼵鼲drupal鼱鼯鼮鼬鼪鼨鼦齉齆鼥鼤龳鼢鼠齊鼜鼛齪龳鼢鼠鼤鼘鼕鼓齪鼑齛鼎drupal8齊鼊齡Session鼆鼅

鼂黾黺黶鼓齪黳黲鼕

黯鼲drupal8齊Session黬龃黫齕Symfony齊Session鼼鼸黪齪鼊齡黧黥龷齕齄 \core\lib\Drupal\Core\Session

龷鼨鼦默鼸黗黖黕齞齞黓黥齕Session齊黐黏黬龃齪黋黓黥齕黉鼪黅黃黅黀黯齊麼麹黬龃齪麶麲麰麬麨黉Session麧黯麤

drupal8龲齭齊Session麠龲齭鼤麜麘麕麓麐麌麈麆齘鼂麃麂鹿鹻齊鹺鹶

鼨鼦鹴鹰鼤龷 Drupal\Core\StackMiddleware\Session 黖鹤鹡齊齪鹠鼥鼤http鹝鹙鹻齊 http_middleware.session

鹏鹋鼂鹇龳龲龮龨鹃齊 龟龞龚Drupal8龄龃龀齽龬HttpKernel鹝鹙齫

麼鸾鸺鼨黖鸸麓齄 $this->Container->get("session"); 鸡鸝鸚龄黬龃麲齄 $session = $this->container->get($this->sessionServiceName);

鸏鸋鸈黺麼黺session鸇鸄齊鸀鷼齄

鼬鼪齕鷺齄 Symfony\Component\HttpFoundation\Session\Session 麧鷴鼦鷱鷰齄

鸇鸄齄 session_manager

鷬鷫鷪齄 Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag

鷢黓鷪齄 Symfony\Component\HttpFoundation\Session\Flash\FlashBag

鸡黋黕鼆鷞鷝麜黺鷜鷘麘鼛鷖齊鼛鷖鸀鷼鷰鷕鷔鹶鼂鹇龳龲龮龨默齊鸇鸄鼛鷖鼢鼠鷐鷎齡龀鸑

鷋鹇齉龳鼬鼪齕Symfony齊Session鼼鼸齪齦黕鷈齊鼤鼬鼪齕黕鷈齊SessionStorage 鷅黓鷂鶾鷖

Session鶽鶾齊黐黏鼥鼤鼨鼦鷅黓鷂鶾鷖齪鶻鼥鼤龳鼢鼠齊鶺鶸齪龷drupal8齊龄黬龃鹻鹰鶴麈鶳齘鼬鼪SessionStorage

鼨鼦鶯鶬黕鼤鶩鶨鶧齪鸺鶻齊鶤鶣鸈黺齪鶢鶡SessionManager鶟麲鶝鶚

麶鶙龷drupal8鹻鸚鶘SessionStorageInterface鶑鶐齊SessionStorage鶎鶋鶯麲齕SessionManager

SessionManager黲鶉鶇Symfony齊NativeSessionStorage齪

鶄鶁鼂鶀鵿SessionManager鼥鼤Symfony鵽鵼鹻齊SessionStorage齪鵹默鼑鵶麲齏齍鷂鶾鷖麤

龷齏齍龲齭鹻麧鷴鼦齡龀齄鷰鷕鶽鶾鷖鸡鷅黓鷰鷕鷪鸑鵴齏齍鷂鶾鷖鸡鵲鵯鵮齡龀鶤鶣鸑鵴齏齍鶽鶾鷖鸡鵲鵯齏齍齊鵪鹒鵩龥鸑

SessionManager鼱鼯黓鷅鷰鷕鼥黺鶻黖鹃麈麆齊SessionHandler齪鼨鼦齏齍鶽鶾鷖鼥鼤Drupal\Core\Session\SessionHandler

鼨鼦SessionHandler麌黕鵥鶑鵣鵟鵝SessionManager

鵛鼤鵗龣鹒鹒鷪鵔齪鵑麼鹒鷪鵔鵐鵍麼鼦鵊黪鶤鶣齪鷪鵔鹋鵆鷝鵹齄

SessionHandler齄鵃黖鹒齊鵂麓齊齏齍鶽鶾鷖

Symfony\Component\HttpFoundation\Session\Storage\Handler\WriteCheckSessionHandler : 鴽麼鹒鷪鵔齪鵐鵍鷫鶣黶齊鴹麐齪鷝鴸鷰鷕鴵鴲鴯鴫黳鴨龥鴤鹰

Drupal\Core\Session\WriteSafeSessionHandler :鴽鴡鴨鷪鵔齪鵐鵍鴟鴝鼤鴚鷋龥齊鶣鴘

鷝鴸龷鴔鴐鹻鼬鼪齄 \Drupal::service('session_handler.write_safe')->setSessionWritable(false); 鼑鴋鴉齏齍鴈鴇鴃黓

鵃鴀鳼鳺鵗龣鳹鴨鷪鵔齊齏齍鶽鶾鷖鵣鵝SessionManager齪龷鸇鸄鸀鷼yaml默鼸黖鹃黺黕鳸鼨鳶鷪鵔黯龲

鸝鸚鼨鼦鷪鵔黯龲鼤龷鼛鷖鳴鳱黖鹃鳰鶘齊齪鳴鳱鷖鳮鼲齄

Drupal\Core\DependencyInjection\Compiler\StackedSessionHandlerPass

鸏鸋黺麼黺SessionHandler齪鶻鳦鸀鳤drupal齏齍鷰鷕鼱鼯鷅黓鵩龥齪

黺黬龃鷋鶁鼆鷞drupal8齊Session鷰鷕麌黕鼤鳠鵂鳝php齥鳜黓龷默鼸龲齭鹻齪

鵛鼤黥龷齕 鷰鷕鳚 黖鹃齪鷰鷕鳗鶯麲齄sessions齪鷈麘鷅黓齊黋麧sessions麂鳔齊鼪黅id鵴龚黅鳑鼢鳎ip鵴麘鳋鳉鵴齏齍鳈鳆id

齏齍鳅鷰鷕MetadataBag齄

MetadataBag 鼪鼲黓黥齏齍龳鳄齊鷬鷫齪drupal齊 MetadataBag 黲鶉齕 symfonyd MetadataBag 齪麲鳁鼯鲽黲鶉鹶麶麲鶻鲼鲸齕麂鲶鲲鼂麃鲮鲫CSRF齊鲦鲢

MetadataBag 黖鹃黓黥齕鲡鲟CSRF齊鴡鴨鲛鲚鳶麠齪鵶麲 CsrfTokenSeed 齪麲鳁鼯鲽鼨鼦鳶麠鼂鲔鲒CSRF鲑

鶘龷鸏鸋鼆鷞齕drupal8齏齍鷰鷕齊黓黥齪鶙黪齏齍鷂鶾鷖鼪齘齕SessionConfiguration齪鼨鲍鲌鲋鲇黕麨齛鼎麤

drupal8齏齍鼘鲆鲃鱿鱻鷫齄

drupal8鱸鱵齏齍鼘鲆鲃鱿鶤鶣齪鼨鼦鼤drupal麲齕鵐鱱鷫鶣鵊黪鱭鲸齊齪龷Symfony鹻麌鱬麧

鷝鴸龷鼂麃鹻鱬麧鱫鶘齏齍id齥鼯黕齏鲃鱿齏齍齪鼨鼦鱩鱥鱡鶯鱟黓鱫鱛鹰鼪齪鱡鶯鱟黓龲齭鷝鴸龷鼂麃鹻鱫鶘齏齍id鱗鶎鱖麲黕鼤鱡鶯齊齪鼑黕鹴鹰

齥鸏鸋鱓鲽鱏鸚齊鲃鱿鼱鼯鱌鹶鱬麧黯龲齪鸏鸋鸝鸚鱬麧鱊鲽鼨鳜鶡齪齏齍麂鹿齏龷鱬麧鲃鱿齊鹋鵆鵹齏鱆鱅$_SESSION

鶄鶁鸏鸋鷋鶁鰼鰸鼬鼪齏齍麂鹿齪鵛黕齏鰴鰱齘鱬麧鱏鰭鲃鱿齪鷝鴸麧齏齍鷰鷕鰪鳝齥鼯齏齍麂鹿龷鴃黓齊麘鰩齏鱏鰭鲃鱿齏齍鼮鴃黓鷰鷕

鸝鵂鶾鷋鶁鼬鼪鰧鰣鼜鼛鰡齊黬龃鰞鰚齄

龷php鹻鳗鰚齏齍鰖鰔麧鷴鼦鰸鰓齄

PHP_SESSION_DISABLED 齏齍鶤鶣鶎鰋鼪麤
PHP_SESSION_NONE 齏齍鶤鶣鷋鼪齪齦黕黓龷鰊龨齏齍鸡鱬麧鲃鼪鸑麤
PHP_SESSION_ACTIVE 齏齍鶤鶣鷋鼪齪鵛鰇黓龷鰊龨齏齍鸡鰅鵗鲃鼪鸑麤

鵹鹃鸏鸋鸈黺麼黺 \core\lib\Drupal\Core\Session 默鼸黗黖鹃黉齏齍黀黯齊鸝鰁黬龃齄

AccountInterface 齄黃黅鶑鶐齪鵐鵍齉龳齊黃黅鯾鯻鶑鶐

UserSession 齄鯶鱖齊黃黅鶑鶐鸚鶘

AnonymousUserSession 齄鱡鶯黃黅鶑鶐

AccountProxyInterface 齄黃黅黬鶾鶑鶐齪鸝鸚鼥鼤麼鼦黃黅麂鹿齊鷪鵔齪麲鳁鼯鲽鯯鯫鰁鹶麶麲鷋鶁鶡黃黅鸾鯩

AccountProxy 齄黃黅黬鶾鶑鶐齊鯶鱖鸚鶘

AccountSwitcherInterface 齄黃黅鸾鯩鶑鶐

AccountSwitcher 齄黃黅鸾鯩鶑鶐齊鯶鱖鸚鶘

PermissionsHashGeneratorInterface 齄鲚鯦鳈鳆鰪鳝鷖鶑鶐

PermissionsHashGenerator 齄鲚鯦鳈鳆鰪鳝鷖

鶁黶鼥鼤drupal8齊齉龳session鼊鯢齕麤

<?php  
echo isset($_SESSION)?"isStart":"noStart"; // 并未调用session_start()显示未开始  
function yunke()  
{  
    $_SESSION=array();//变量名$_SESSION具有特殊性,可用超全局,如果圆通$_YUNKE就不行  
}  
yunke();  
echo isset($_SESSION)?"isStart":"noStart"; //超全局会话对象存在了,虽然有$_SESSION超全局变量存在,但会话并没有真正启动,并未调用session_start()  
echo session_status (); //返回PHP_SESSION_NONE  

额外信息

1齄龷鲲鶸鯚鯖默鼸黖鹃鷋鶁鯯鯖齏齍齊鯓鯒鶟鯎鯊鯈齪鹠鼥鼤麨鯇麘鳋鱓鲽鶟鯎麼鴨齏齍鷰鷕齊鵃鴀鯓鯒麘鳋

鼨鼦齍麧鶸鯅齪鸝鸚鼥鼤龷齏齍齊鳅鷰鷕鷪黖鹃鶀鯃齕齏齍齊鵃鴀鼬鼪麘鳋齪龄黬龃齄$this->meta[self::UPDATED]齪鼨鼦麘鳋鴃黓龷$_SESSION黖

鸸鲃齏齍齊鼪黅鷋鶣鮸鼕鮷鸸麨鼦鮳鹃齪鵑鼦鮳鹃黧齏鯓鯒齏齍齪鵛齏齍鷰鷕鮱鱬麧鴲麐齪鴈鴲麐鼤黕齏鶺龥齏齍齊

齥鼯鵑鴨黧鼮鶀鯃麼鵹鯓鯒麘鳋齪鼥鴋鴉齏齍$_SESSION鮭鴲齪鱓鲽鷰鷕鳚鮸鶑鶺龥齏齍鷰鷕齪鼨鳜鮬麧麹鮫鮧鷫鶣

齥鼯鼥鯯鸀麼鼦鯊鯈齪鮤龣鼨鼦鯊鯈麘鳋鳼鶀鯃麼鴨齪鼨鳜鼥麧鲍鲌鮢齊鷫鶣齪龷齏齍鴈鮭鴲齊鹋鵆鵹黕鼪鵑鴨黧鶺龥鷰鷕

鮟鮛鷝鵹齄

$settings['session_write_interval'] =鮘鷰鮗鯶鱖麲180鮘齪

2齄鯶鱖齊齏齍鷅黓鮑鮍鼤齄

gc_probability : 1 鮌鵹麼鮍鶝鼪
gc_divisor : 100 鮌黶鹃齊鮑鮍麼鮊鳦鸀齏齍鸸麓麘鲃鱿鮇鮄鮂鮁魾魽齊鵽魼
gc_maxlifetime : 200000 魻鶋鮤龣魸鯈齊齏齍齏鶎魵黫齪龷齏齍鶽鶾鷖鹻齊gc($lifetime)鼥鼤鼪齊魸鯈齪鮤魴齏龷鷰鷕鳚鹻魲黫

cookie_lifetime : 2000000 鶁鮘鷰魱鸀齕鱫魯齘魬魩鷖齊 cookie 齊鳝鶋魨魴麤鯈麲 0 鳗鰚魦鵥齘黯魣魬魩鷖魟麤鯶鱖麲 0

鼨鼦鯶鱖鯈鷋鶁龷 /sites/default/services.yml 鹻魙鮭齪鸡麂鳔齘鶇魕齊鲲鶸魑鯃齪鷝鴸鱬麧 services.yml 默鼸鼂魎鴝麼魊 default.services.yml 魙鮭麲 services.yml 鸑

黯鼲鶟麨齏齍鮑鮍鼂魇黺魅魃鬿鬾齄 http://php.net/manual/zh/session.configuration.php

3:鷝鴸鬺鵥鶑鸺鷰鷕鳚鵹鬹齏齍鷰鷕齪鼬鼪unserialize魃鴇齏鰪鳝鬶鬲齪鼨鼤鬮鼲齏齍鷰鷕齊鬬龮麐黕鷈鼲serialize魃鴇齪鷋鬮 session.serialize_handler 魱鸀

鷝鴸鬺鬨鮢鬦鬣黺麼黺鷰鷕鳚黖鹃鴃黓齊齏齍鷰鷕鼂鼪鰧鰣鼜鼛鰡齊魃鴇鬢鬬龮麐齄

4齄鷝麜鼬鼪drupal8鶇魕齊齏齍麂鹿齄

麧鼛鷖麘齄 $this->container->get("session");

麧鼂麃麂鹿麘齄 $request->getSession();

鼊鬝鼬鼪齄 \Drupal::service('session');

5齄drupal齊齏齍龲齭黉鴽鷴魃鼤黕鬚鼛齊齪鹠鼥鼤鬙鷝鴸鴽鷴魃魽鬬鰅鵗鸸鲃齏齍齪齥鼯鼬鼪drupal齏齍鼑鬗鬶齪鼂鶀鵿鱊鱓鬮drupal鸈鸸鲃齏齍

鹠黉php.ini 鹻齊鶇鱿鸸麓魱鬖 session.auto_start = 1 黕鬚鼛齪魸魱鬖鱊鬕鯯鯖麲0黯魣

drupal齊齏齍鷰鷕鼤黥鷰鷕鳚齊齪鴽鷴魃魽鬬鷋鶣黕鶣齪鵛php鬔鶣麈鬾麼鼦 SessionHandler 鴃黓鶽鶾鷖齪鼨鼤黕鬚鼛齊鬑龄

鼨鳜齊黕鬚鼛鷫龷Symfony鹻麧鬍鬋齪鵝鳸齕鬇鳦鱌鴇齪鼂黺齄 http://symfony.com/doc/current/components/http_foundation/session_php_b…

session_start();  
session_decode(file_get_contents("sessions-session.bin"));  
print_r($_SESSION); 

本书共63小节:

评论 (写第一个评论)