Laravel 会话在原生 PHP 中不可用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14428785/
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 sessions not available in native PHP?
提问by NightMICU
New to Laravel and having some problems with Sessions. Specifically, reading session data from a PHP file outside of Laravel.
Laravel 新手并且在使用 Sessions 时遇到了一些问题。具体来说,就是从 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 -
例如,假设我像这样设置会话变量:Session::put('isAuthorized', 'yes')
- 我可以在 Laravel 上下文中很好地检索它,Session::get('isAuthorized')
但以下 PHP 不会检索此会话密钥 -
<?php
session_start();
echo $_SESSION['isAuthorized'];
?>
returns
回报
Notice: Undefined index: isAuthorized in C:\xampp\htdocs\session.php on line 3
I have tried setting the Laravel session driver to both the default cookie
and file
modes, same result.
我尝试将 Laravel 会话驱动程序设置为默认cookie
和file
模式,结果相同。
采纳答案by JamesG
Please note that this answer is specific to Laravel 3
请注意,此答案特定于 Laravel 3
Laravel doesn't use PHP sessions, so forget session_start()
, $_SESSION
, etc.
Laravel不使用PHP会话,就这么算了session_start()
,$_SESSION
等
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.
如果您使用文件会话驱动程序运行,则会话数据存储在 storage/sessions 中的文件中。您可以通过从 cookie 中读取 Laravel 会话 ID 来获取文件的名称。因此,解决您的问题的黑客方法是编写一些代码,从 cookie 中获取会话 ID,然后在 storage/sessions 文件夹中查找具有该名称的文件,读取该文件,json_decode()
然后您就可以读取整个事物。
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.
如果您使用 cookie 会话驱动程序运行,所有会话数据都存储在 cookie 中,但它是加密的,因此您必须拥有密钥的副本(应该在 application/config/application.php ) 然后找出 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.
为了实现您希望实现的目标 - 即确定当前人员是否已获得授权,最好在您的应用程序中构建一个 API 并对其进行保护,使其只能由本地主机访问。从性能的角度来看,这不是一个很好的解决方案,但可能更优雅,因为您没有在 Laravel 会话管理的内部进行黑客攻击。
回答by itsjavi
You could also write a session adapter, so the $_SESSION
variable will be an instance of it:
您还可以编写一个会话适配器,因此该$_SESSION
变量将是它的一个实例:
<?php
class SessionAdapter implements \ArrayAccess {
public function offsetExists($offset) {
return Session::has($offset);
}
public function offsetGet($offset) {
return Session::get($offset);
}
public function offsetSet($offset, $value) {
return Session::put($offset, $value);
}
public function offsetUnset($offset) {
return Session::forget($offset);
}
}
And then somewhere in your code:
然后在您的代码中的某处:
<?php
$_SESSION = new SessionAdapter();
// or
$GLOBALS['_SESSION'] = new SessionAdapter();
This way native PHP session and Laravel session will be "the same".
这样本机 PHP 会话和 Laravel 会话将是“相同的”。
回答by Jeroen
Laravel uses storage drivers for its sessions, namely cookie, file, database, memory, memcached and redis (and APC in Laravel 4).
Laravel 为其会话使用存储驱动程序,即 cookie、文件、数据库、内存、memcached 和 redis(以及 Laravel 4 中的 APC)。
The web is a stateless environment. This means that each request to your application is considered unrelated to any previous request. However, sessions allow you to store arbitrary data for each visitor to your application. The session data for each visitor is stored on your web server, while a cookie containing a session ID is stored on the visitor's machine. This cookie allows your application to "remember" the session for that user and retrieve their session data on subsequent requests to your application.
网络是一个无状态的环境。这意味着对您的应用程序的每个请求都被视为与之前的任何请求无关。但是,会话允许您为应用程序的每个访问者存储任意数据。每个访问者的会话数据都存储在您的网络服务器上,而包含会话 ID 的 cookie 存储在访问者的机器上。此 cookie 允许您的应用程序“记住”该用户的会话,并在对您的应用程序的后续请求中检索他们的会话数据。
http://laravel.com/docs/session/config
http://laravel.com/docs/session/config
The default storage driver is Cookie, so try this:
默认存储驱动程序是 Cookie,所以试试这个:
print_r($_COOKIE);
回答by Edwin Aw
Session handling in Laravel is indeed different from native PHP session. To use native PHP session, set the value as below:
Laravel 中的会话处理确实与原生 PHP 会话不同。要使用本机 PHP 会话,请设置如下值:
<?php
session_start();
$_SESSION['isAuthorized'] = 'yes';
echo $_SESSION['isAuthorized']; // output yes
?>