Skip to content

Instantly share code, notes, and snippets.

@jcbagtas
Last active August 23, 2021 12:11
Show Gist options
  • Save jcbagtas/0e981885e72e383e0f0d582db14de8af to your computer and use it in GitHub Desktop.
Save jcbagtas/0e981885e72e383e0f0d582db14de8af to your computer and use it in GitHub Desktop.
Get current Laravel active session from outside laravel instance. Laravel 5.2
<?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();
}
@jcbagtas
Copy link
Author

USAGE
From any php file outside the laravel instance:

getLaravelUser()->name;

@damms005
Copy link

damms005 commented Jan 7, 2020

Genius!

@jcbagtas
Copy link
Author

Genius!

Glad it still helps people 4 years later. ^_^

@julianphp
Copy link

@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