Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

php - How can I get the session ID in Laravel?

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: IlluminateSupportFacadesSession::$instance

have tried:

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

but to no avail.. any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...