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

本书共78小节:

评论 (写第一个评论)