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); 

本书共88小节:

评论 (写第一个评论)