问题描述
我想在Laravel 4中获取会话的ID
I want to get the id of the session in Laravel 4
在Laravel 3中,它有点hacky,但是可以通过以下代码实现:
In Laravel 3, it was a bit hacky, but possible with this code:
$session_id = Session::$instance->session['id'];
但是我在Laravel 4中无法解决...引用Session::$instance
对象会引发错误:
But I can't work it out in Laravel 4... referencing the Session::$instance
object throws errors:
尝试过:
$session_id = Session::get('id');
但无济于事..有什么想法吗?
but to no avail.. any ideas?
推荐答案
取决于Laravel的版本:
Depends on the version of Laravel:
$session_id = $_COOKIE["laravel_session"];
Laravel 4.0:
不是4.1及更高版本:
$session_id = session_id(); //thanks Phill Sparks
Laravel 4.1(及更高版本):
$session_id = Session::getId();
或
$session_id = session()->getId()
Laravel不再直接使用内置的PHP会话,因为开发人员可以选择使用各种驱动程序来管理访问者的会话( Laravel 4.2 , Laravel 5.1 , Laravel 5.2 ).
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的Session
门面或session()
帮助器来获取会话的ID:
Because of this, now we must use Laravel's Session
facade or session()
helper to get the ID of the session:
这篇关于如何在Laravel中获取会话ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!