Laravel 会话立即过期(Laravel 5.5)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46796074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel session expires immediately (Laravel 5.5)
提问by Hüseyin Dursun
I have created my custom model to log in the users. I am using sqlite as database. "users" table has "id" column and I am not using eloquent so I have changed the driver as "database" and table as "users" in auth.config file.
我已经创建了我的自定义模型来登录用户。我使用 sqlite 作为数据库。“users”表有“id”列,我没有使用eloquent,所以我在auth.config文件中将驱动程序更改为“数据库”,将表更改为“用户”。
My index controller checks the user if it is logged in. If not, redirects to the login page. Once the login form is submitted (ajax call), if the user enter wrong username or password, it gets error message in a modal telling it has entered wrong credentials. If the username and password are true, it redirects to the index page. But because the user is not assumed as logged in, again it redirects to the login page. Also I tried to echo the id of the user after the successful attempt (without redirecting), and it displayed without problem. But after visiting the index page, I am redirected to login page again.
我的索引控制器检查用户是否已登录。如果没有,则重定向到登录页面。一旦提交登录表单(ajax 调用),如果用户输入错误的用户名或密码,它会在模式中收到错误消息,告诉它输入了错误的凭据。如果用户名和密码为真,则重定向到索引页面。但是因为用户未被假定为已登录,所以它再次重定向到登录页面。我还尝试在成功尝试后回显用户的 id(没有重定向),并且显示没有问题。但是在访问索引页面后,我再次被重定向到登录页面。
I have tried to add session and display it so if I call the following code;
我试图添加会话并显示它,所以如果我调用以下代码;
session()->put('foo', 'bar');
echo session()->get('foo');
"bar" is echoed on the page. But if I echo the session on another page (without putting a new session because I have already added), "bar" is not echoed on the page.
“bar”在页面上回显。但是,如果我在另一个页面上回显会话(没有放置新会话,因为我已经添加了),“bar”不会在页面上回显。
Also note that no files are stored in "storage/framework/sessions" folder.
另请注意,“storage/framework/sessions”文件夹中未存储任何文件。
Session type is file and lifetime is 720.I have also tried "cookie" and "database". The same problem occured with "cookie" but I got "database driver cannot be used" exception with "database" driver (I have successfully migrated the necessary tables).
会话类型是文件,生命周期是 720。我也试过“cookie”和“数据库”。“cookie”也出现了同样的问题,但“数据库”驱动程序出现“数据库驱动程序无法使用”异常(我已成功迁移了必要的表)。
Also the website is on localhost (Windows 10). So I do not think folder permissions can be a problem. Also I have another Laravel (older version - it must be 5.4 I think) on the same pc and it is working without problem. Both are using php 7 and nginx server.
该网站也位于本地主机(Windows 10)上。所以我认为文件夹权限不是问题。此外,我在同一台电脑上还有另一个 Laravel(旧版本 - 我认为它必须是 5.4)并且它可以正常工作。两者都使用 php 7 和 nginx 服务器。
回答by Hüseyin Dursun
So I have found my problem. I had created a custom ServiceProvider and defined custom routes to create modular system. Because I tried to "include" route files here, new laravel routing system acted it as api routes. So sessions were not available.
所以我发现了我的问题。我创建了一个自定义 ServiceProvider 并定义了自定义路由来创建模块化系统。因为我试图在这里“包含”路由文件,新的 Laravel 路由系统将其作为 api 路由。所以会话不可用。
Now I have changed my code to:
现在我已将代码更改为:
Route::middleware('web')->group($route_file);
and routes are behaved as web routes and sessions are working.
和路由的行为就像网络路由和会话正在工作一样。
Hope to see this helps others having this issue.
希望看到这可以帮助其他遇到此问题的人。
回答by Lloople
Check session configuration on file config/session.php
.
检查文件上的会话配置config/session.php
。
If your session driver is file, make sure you have permissions on storage/session
folder with chmod -R 777 storage
.
如果您的会话驱动程序是文件,请确保您对storage/session
带有chmod -R 777 storage
.
Also you can check for lifetime
and expire_on_close
configurations on that file to preserve session between requests.
您还可以检查lifetime
和expire_on_close
对文件的配置,以请求之间保持会话。