Laravel会话在本机PHP中不可用? [英] Laravel sessions not available in native PHP?

查看:71
本文介绍了Laravel会话在本机PHP中不可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Laravel的新手,并且在Sessions方面遇到一些问题.具体来说,是从Laravel外部的PHP文件读取会话数据.

New to Laravel and having some problems with Sessions. Specifically, reading session data from a PHP file outside of Laravel.

例如,假设我这样设置会话变量:Session::put('isAuthorized', 'yes')-我可以使用Session::get('isAuthorized')在Laravel上下文中很好地检索此变量,但以下PHP不会检索此会话密钥-

For example, let's say I set the session variable like so: Session::put('isAuthorized', 'yes') - I can retrieve this just fine in the Laravel context with Session::get('isAuthorized') but the following PHP will not retrieve this session key -

<?php
session_start();
echo $_SESSION['isAuthorized'];
?>

返回

Notice: Undefined index: isAuthorized in C:\xampp\htdocs\session.php on line 3

我尝试将Laravel会话驱动程序设置为默认的cookiefile模式,结果相同.

I have tried setting the Laravel session driver to both the default cookie and file modes, same result.

推荐答案

请注意,此答案特定于Laravel 3

Laravel不使用PHP会话,因此请忘记session_start()$_SESSION等.

Laravel doesn't use PHP sessions, so forget session_start(), $_SESSION, etc.

如果使用文件会话驱动程序运行,则会话数据将存储在文件中的存储/会话中.您可以通过从cookie中读取Laravel会话ID来获取文件名.因此,解决问题的简便方法是编写一些代码,以从Cookie中获取会话ID,然后在storage/sessions文件夹中查找具有该名称的文件,然后在文件中读取该文件,然后按json_decode()键即可.阅读全部内容.

If you're running with file session driver, the session data is stored in a file in storage/sessions. You can obtain the name of the file by reading the Laravel session ID from the cookie. So the hacky way to solve your problem would be to write some code that obtains the session ID from the cookie and then looks for the file with that name in the storage/sessions folder, read that file in, json_decode() it and you can read the whole thing.

如果您正在使用cookie会话驱动程序运行,则所有会话数据都存储在cookie中,但是已加密,因此您必须拥有密钥的副本(应该在application/config/application.php),然后找出Laravel使用的是哪种加密方法,以便可以对其进行解密.然后,您可以读取所有会话变量.

If you're running with cookie session driver, all of the session data is stored in the cookie, but it is encrypted, so you'd have to have a copy of the key (which should be in application/config/application.php) and then figure out what encryption method Laravel is using so you can decrypt it. Then you can read all the session variables.

要实现您希望实现的目标-也就是说,确定当前人员是否被授权,最好在应用程序中构建一个API并对其进行保护,以使其只能由localhost访问.从性能的角度来看,这不是一个很好的解决方案,但可能会更优雅,因为您不会在Laravel会话管理的内部进行操练.

To achieve what you're hoping to achieve - that is, figure out if the current person is authorized, it might be better to build an API into your app and secure it so that it can only be accessed by localhost. Not a great solution from a performance standpoint, but potentially more elegant because you're not hacking around with the internals of Laravel session management.

这篇关于Laravel会话在本机PHP中不可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆