php 如何在 Laravel 中获取会话 ID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16812747/
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
How can I get the session ID in Laravel?
提问by msturdy
I want to get the id of the session in Laravel 4
我想在 Laravel 4 中获取会话的 ID
In Laravel 3, it was a bit hacky, but possible with this code:
在 Laravel 3 中,它有点 hacky,但可以使用以下代码:
$session_id = Session::$instance->session['id'];
But I can't work it out in Laravel 4... referencing the Session::$instance
object throws errors:
但我无法在 Laravel 4 中解决它......引用Session::$instance
对象会引发错误:
Access to undeclared static property: Illuminate\Support\Facades\Session::$instance
访问未声明的静态属性:Illuminate\Support\Facades\Session::$instance
have tried:
试过:
$session_id = Session::get('id');
but to no avail.. any ideas?
但无济于事..有什么想法吗?
回答by msturdy
Depends on the version of Laravel:
取决于 Laravel 的版本:
Laravel 3:
Laravel 3:
$session_id = $_COOKIE["laravel_session"];
Laravel 4.0:
Laravel 4.0:
Just not versions 4.1 and above:
只是不是 4.1 及更高版本:
$session_id = session_id(); //thanks Phill Sparks
Laravel 4.1 (and onwards):
Laravel 4.1(及以后):
$session_id = Session::getId();
or
或者
$session_id = session()->getId()
Laravel no longer uses the built-in PHP sessions directly, as a developer could choose to use various drivers to manage the sessions of visitors (docs for Laravel 4.2, Laravel 5.1, Laravel 5.2).
Laravel 不再直接使用内置的 PHP 会话,因为开发人员可以选择使用各种驱动程序来管理访问者的会话(Laravel 4.2、Laravel 5.1、Laravel 5.2 的文档)。
Because of this, now we must use Laravel's Session
facade or session()
helper to get the ID of the session:
因此,现在我们必须使用 Laravel 的Session
facade 或session()
helper 来获取会话的 ID:
回答by Alex Regenbogen
Just had a little hunt for myself, because the above answers didn't work for me (Laravel 4.1)...
只是对自己进行了一些搜索,因为上述答案对我不起作用(Laravel 4.1)...
Had to use this:
不得不使用这个:
Session::getId();
回答by Ahmed Mahmoud
in laravel 5.5 first write this in your controller use Session;
and inside your controller function write the following linesession()->getId();
在 Laravel 5.5 中,首先将其写入您的控制器use Session;
并在您的控制器函数中写入以下行session()->getId();