Last active
August 23, 2021 12:11
-
-
Save jcbagtas/0e981885e72e383e0f0d582db14de8af to your computer and use it in GitHub Desktop.
Get current Laravel active session from outside laravel instance. Laravel 5.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get current Laravel active session from outside laravel instance. | |
* Tested Laravel 5.2 | |
* | |
* | |
*/ | |
function getLaravelUser() | |
{ | |
require __DIR__ . '\path\to\bootstrap\autoload.php'; | |
require __DIR__ . '\path\to\bootstrap\app.php'; | |
$app->make('Illuminate\Contracts\Http\Kernel')->handle(Illuminate\Http\Request::capture()); | |
$id = $app['encrypter']->decrypt($_COOKIE[$app['config']['session.cookie']]); | |
$app['session']->driver()->setId($id); | |
$app['session']->driver()->start(); | |
return $app['auth']->user(); | |
} |
Genius!
Genius!
Glad it still helps people 4 years later. ^_^
@jcbagtas
In Laravel 8 on line 15 is showing a execption: unserialize(): Error at offset 0 of 81 bytes
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
USAGE
From any php file outside the laravel instance: