3 回答
TA贡献1810条经验 获得超4个赞
哈哈,我之前也是被这个搞蒙了很长一段时间。
不过后来解决了,很简单,不在写入session的地方使用dd
,dump
等方法。
laravel保存session是在中间件中进行的,源代码如下
public function handle($request, Closure $next)
{
$this->sessionHandled = true;
// If a session driver has been configured, we will need to start the session here
// so that the data is ready for an application. Note that the Laravel sessions
// do not make use of PHP "native" sessions in any way since they are crappy.
if ($this->sessionConfigured()) {
$session = $this->startSession($request);
$request->setSession($session);
}
$response = $next($request);
// Again, if the session has been configured we will need to close out the session
// so that the attributes may be persisted to some storage medium. We will also
// add the session identifier cookie to the application response headers now.
if ($this->sessionConfigured()) {
$this->storeCurrentUrl($request, $session);
$this->collectGarbage($session);
$this->addCookieToResponse($response, $session);
}
return $response;
}
保存session的function
protected function addCookieToResponse(Response $response, SessionInterface $session)
{
if ($this->usingCookieSessions()) {
$this->manager->driver()->save();
}
if ($this->sessionIsPersistent($config = $this->manager->getSessionConfig())) {
$response->headers->setCookie(new Cookie(
$session->getName(), $session->getId(), $this->getCookieExpirationDate(),
$config['path'], $config['domain'], Arr::get($config, 'secure', false)
));
}
}
可以看到,保存session的方法在 $response = $next($request)
之后进行的,你在你的代码中使用了dd()、dump()、exit()
等方法,后面的保存session的代码将不能执行。
TA贡献1828条经验 获得超13个赞
Laravel的session机制是:在程序的运行的过程中是把你的对session的操作用一个类记录在内存中,然后在response发送给用户以后执行session中间中的terminate方法把session数据持久化到相应的介质中去。如果在windows和linux下面没问题,到了mac下就出了题很有可能是最后持久化的时候出了问题。
你是否更改了存储介质,比如从redis变到了文件。那么那个文件有没有写的权限?要给storage目录足够的权限
如果是用内存存储的session那么redis或者memerycache是否配置正确?
还有就像楼上说的那样,不要用dd,因为dd完之后会终止程序,session就不会持久化,只是将运行内存中的值给你打印出来了而已。
还有一个debug方法,在Session::put()之后加一句
Session::save();
这句代码是手动持久化session。如果成功说明你的session持久化没问题,只是你程序运行的时候没有到持久化这一步。
如果失败回报失败的原因。
有一次我遇到了session写不进去是因为硬盘满了...
TA贡献1818条经验 获得超3个赞
从代码上来看应该没问题,很简单的逻辑,而且在 windows 和 linux 上都是正常的。
个人再提供个思路:看下在 chrome Application 的 laravel_session 值。
- 3 回答
- 0 关注
- 439 浏览
添加回答
举报