How can I get the session ID in Laravel?

牧云@^-^@ 提交于 2019-12-17 16:02:12

问题


I want to get the id of the session in Laravel 4

In Laravel 3, it was a bit hacky, but possible with this code:

$session_id = Session::$instance->session['id'];

But I can't work it out in Laravel 4... referencing the Session::$instance object throws errors:

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

have tried:

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

but to no avail.. any ideas?


回答1:


Depends on the version of Laravel:

Laravel 3:

$session_id = $_COOKIE["laravel_session"];

Laravel 4.0:

Just not versions 4.1 and above:

$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).

Because of this, now we must use Laravel's Session facade or session() helper to get the ID of the session:




回答2:


Just had a little hunt for myself, because the above answers didn't work for me (Laravel 4.1)...

Had to use this:

Session::getId();



回答3:


in laravel 5.5 first write this in your controller use Session; and inside your controller function write the following linesession()->getId();



来源:https://stackoverflow.com/questions/16812747/how-can-i-get-the-session-id-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!