126 $this->appState = $appState;
136 session_write_close();
149 if (!in_array(substr(
$method, 0, 3), [
'get',
'set',
'uns',
'has'])) {
150 throw new \InvalidArgumentException(
151 sprintf(
'Invalid method %s::%s(%s)', get_class($this),
$method, print_r($args, 1))
154 $return = call_user_func_array([$this->storage,
$method], $args);
155 return $return === $this->storage ? $this : $return;
167 \Magento\Framework\Profiler::start(
'session_start');
170 $this->appState->getAreaCode();
171 }
catch (\
Magento\Framework\Exception\LocalizedException $e) {
172 throw new \Magento\Framework\Exception\SessionException(
174 'Area code not set: Area code must be set before starting a session.' 181 $this->initIniOptions();
183 if (isset($_SESSION[
'new_session_id'])) {
186 session_id($_SESSION[
'new_session_id']);
188 $sid = $this->sidResolver->getSid($this);
192 if (isset($_SESSION[
'destroyed'])
193 && $_SESSION[
'destroyed'] <
time() - $this->sessionConfig->getCookieLifetime()
195 $this->
destroy([
'clear_storage' =>
true]);
198 $this->validator->validate($this);
199 $this->renewCookie($sid);
201 register_shutdown_function([$this,
'writeClose']);
204 \Magento\Framework\Profiler::stop(
'session_start');
206 $this->storage->init(isset($_SESSION) ? $_SESSION : []);
216 private function renewCookie($sid)
223 $cookieValue = $sid ?: $this->cookieManager->getCookie($this->
getName());
225 $metadata = $this->cookieMetadataFactory->createPublicCookieMetadata();
226 $metadata->setPath($this->sessionConfig->getCookiePath());
227 $metadata->setDomain($this->sessionConfig->getCookieDomain());
228 $metadata->setDuration($this->sessionConfig->getCookieLifetime());
229 $metadata->setSecure($this->sessionConfig->getCookieSecure());
230 $metadata->setHttpOnly($this->sessionConfig->getCookieHttpOnly());
232 $this->cookieManager->setPublicCookie(
250 [$this->saveHandler,
'open'],
251 [$this->saveHandler,
'close'],
252 [$this->saveHandler,
'read'],
253 [$this->saveHandler,
'write'],
254 [$this->saveHandler,
'destroy'],
255 [$this->saveHandler,
'gc']
279 public function getData($key =
'', $clear =
false)
281 $data = $this->storage->getData($key);
282 if ($clear && isset(
$data)) {
283 $this->storage->unsetData($key);
305 return session_name();
341 if (
$options[
'send_expire_cookie']) {
353 $this->storage->unsetData();
364 return $this->sessionConfig->getCookieDomain();
374 return $this->sessionConfig->getCookiePath();
384 return $this->sessionConfig->getCookieLifetime();
396 if ($sessionId !==
null && preg_match(
'#^[0-9a-zA-Z,-]+$#', $sessionId)) {
397 session_id($sessionId);
411 $httpHost = $this->request->getHttpHost();
416 $urlHostArr = explode(
'/', $urlHost, 4);
417 if (!empty($urlHostArr[2])) {
418 $urlHost = $urlHostArr[2];
420 $urlPath = empty($urlHostArr[3]) ?
'' : $urlHostArr[3];
422 if (!isset(self::$urlHostCache[$urlHost])) {
423 $urlHostArr = explode(
':', $urlHost);
424 $urlHost = $urlHostArr[0];
426 self::$urlHostCache[$urlHost] = $sessionId;
440 $hostArr = explode(
':', $host);
442 return !empty($hosts[$hostArr[0]]);
454 if ($cookiePath ==
'/') {
458 $urlPath = trim(
$path,
'/') .
'/';
459 return strpos($urlPath, $cookiePath) === 0;
469 $host = $this->request->getHttpHost();
475 $hosts[$host] =
true;
497 unset($_SESSION[self::HOST_KEY]);
515 $newSessionId = session_id();
516 $_SESSION[
'new_session_id'] = $newSessionId;
519 $_SESSION[
'destroyed'] =
time();
525 $oldSession = $_SESSION;
528 session_id($newSessionId);
530 $_SESSION = $oldSession;
533 unset($_SESSION[
'destroyed']);
534 unset($_SESSION[
'new_session_id']);
539 $this->storage->init(isset($_SESSION) ? $_SESSION : []);
541 if ($this->sessionConfig->getUseCookies()) {
554 foreach (array_keys($this->
_getHosts()) as $host) {
556 if ($this->sessionConfig->getCookieDomain() !== $host) {
557 $metadata = $this->cookieMetadataFactory->createPublicCookieMetadata();
558 $metadata->setPath($this->sessionConfig->getCookiePath());
559 $metadata->setDomain($host);
560 $metadata->setSecure($this->sessionConfig->getCookieSecure());
561 $metadata->setHttpOnly($this->sessionConfig->getCookieHttpOnly());
562 $this->cookieManager->deleteCookie($this->
getName(), $metadata);
576 if (!$this->sessionConfig->getUseCookies()) {
580 $metadata = $this->cookieMetadataFactory->createPublicCookieMetadata();
581 $metadata->setPath($this->sessionConfig->getCookiePath());
582 $metadata->setDomain($this->sessionConfig->getCookieDomain());
583 $metadata->setSecure($this->sessionConfig->getCookieSecure());
584 $metadata->setHttpOnly($this->sessionConfig->getCookieHttpOnly());
585 $this->cookieManager->deleteCookie($this->
getName(), $metadata);
594 private function initIniOptions()
598 $error = error_get_last();
599 throw new \InvalidArgumentException(
600 sprintf(
'Failed to set ini option session.use_only_cookies to value 1. %s', $error[
'message'])
604 foreach ($this->sessionConfig->getOptions() as
$option =>
$value) {
605 if (
$option==
'session.save_handler') {
610 $error = error_get_last();
611 throw new \InvalidArgumentException(
612 sprintf(
'Failed to set ini option "%s" to value "%s". %s',
$option,
$value, $error[
'message'])
ini_set($varName, $newValue)
session_set_save_handler()
clearSubDomainSessionCookie()
__construct(\Magento\Framework\App\Request\Http $request, SidResolverInterface $sidResolver, ConfigInterface $sessionConfig, SaveHandlerInterface $saveHandler, ValidatorInterface $validator, StorageInterface $storage, \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager, \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory, \Magento\Framework\App\State $appState)
getData($key='', $clear=false)
session_regenerate_id($var)
destroy(array $options=null)
getSessionIdForHost($urlHost)
if(!isset($_GET['name'])) $name