如何在Laravel获得会话ID ?

时间:2021-05-07 04:16:42

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中,它有点粗糙,但可能有以下代码:

$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餐厅解决这个问题……引用会话:$instance对象会抛出错误:

Access to undeclared static property: Illuminate\Support\Facades\Session::$instance

访问未声明的静态属性:illumin\ Facades\Session: $instance

have tried:

尝试:

$session_id = Session::get('id');

but to no avail.. any ideas?

但无济于事。什么好主意吗?

3 个解决方案

#1


63  

Depends on the version of Laravel:

取决于Laravel的版本:

Laravel 3:

$session_id = $_COOKIE["laravel_session"];

Laravel 4.0:

Just not versions 4.1 and above:

4.1及以上版本:

$session_id = session_id(); //thanks Phill Sparks

Laravel 4.1 (and onwards):

$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的会话facade或Session () helper来获取会话的ID:

#2


33  

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();

#3


1  

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中,首先在控制器使用会话中写入;在控制器函数中,写入以下的linesession()->getId();

#1


63  

Depends on the version of Laravel:

取决于Laravel的版本:

Laravel 3:

$session_id = $_COOKIE["laravel_session"];

Laravel 4.0:

Just not versions 4.1 and above:

4.1及以上版本:

$session_id = session_id(); //thanks Phill Sparks

Laravel 4.1 (and onwards):

$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的会话facade或Session () helper来获取会话的ID:

#2


33  

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();

#3


1  

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中,首先在控制器使用会话中写入;在控制器函数中,写入以下的linesession()->getId();