Last active
July 4, 2024 12:56
-
-
Save frzsombor/ddd0e11f93885060ef35 to your computer and use it in GitHub Desktop.
Share Laravel's session and check authentication from external projects
This file contains hidden or 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 | |
/* | |
|-------------------------------------------------------------------------- | |
| Sharing Laravel's session and checking authentication | |
|-------------------------------------------------------------------------- | |
| | |
| Use the following code in any CMS (WordPress, Joomla, etc), filemanager (CKFinder, | |
| KCFinder, simogeos's Filemanager, etc), or any other non-Laravel project to boot into | |
| the Laravel framework, with session support, and check if the user is authenticated. | |
| | |
| The following code is tested with Laravel 4.2.11 | |
| It may not work with Laravel 5 | |
| | |
| Last update: 2015-01-09 | |
| | |
*/ | |
require '/path/to/laravel/bootstrap/autoload.php'; | |
$app = require_once '/path/to/laravel/bootstrap/start.php'; | |
$request = $app['request']; | |
$client = (new \Stack\Builder) | |
->push('Illuminate\Cookie\Guard', $app['encrypter']) | |
->push('Illuminate\Cookie\Queue', $app['cookie']) | |
->push('Illuminate\Session\Middleware', $app['session'], null); | |
$stack = $client->resolve($app); | |
$stack->handle($request); | |
$isAuthorized = Auth::check(); | |
Laravel 5.5
require __DIR__.'/../../../vendor/autoload.php'; $app = require __DIR__.'/../../../bootstrap/app.php'; $app->make('Illuminate\Contracts\Http\Kernel') ->handle(Illuminate\Http\Request::capture()); $value = Session::get("any key");
Use Session::all() to see what keys are available:
$value = Session::all();
echo "<pre>";
print_r($value);
echo "<pre>";
But in 5.5 I am still getting empty array for Session and for Auth ... so not pulling the session over for some reason :(
Anyone an idea for a laravel 11 project?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can we achieve this if both applications are on different servers?