Skip to content

Instantly share code, notes, and snippets.

@hassanuos
Last active June 23, 2021 10:29
Show Gist options
  • Save hassanuos/27f699b5c65c18a3b5b5bfc284c64713 to your computer and use it in GitHub Desktop.
Save hassanuos/27f699b5c65c18a3b5b5bfc284c64713 to your computer and use it in GitHub Desktop.
Laravel 7.3 Session Access Outside Laravel 7.3 Directory
// I have one native php project and i want to shift session from Laravel 7.3 to Native old PHP 5.6 and i found this method Its working example
// Please replace your cookie name here below in the code YOUR_LARAVEL_COOKIE_ID `line 16`.
// Also please change the $base64_key get it from your laravel .env file line 14.
// `user` thats my custom session key on laravel and i get all the data line 28.
try {
require_once($_SERVER['DOCUMENT_ROOT'] . "/site/home/vendor/autoload.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/site/home/bootstrap/app.php");
$kernel = $app->make('Illuminate\Contracts\Http\Kernel');
$response = $kernel->handle($request = Illuminate\Http\Request::capture());
$base64_key = "base64:CkwNrVEX73T8Kqm09z95V18Ewr0sqSP+P6kFYG1coSs=";
$payload = json_decode(base64_decode($_COOKIE['YOUR_LARAVEL_COOKIE_ID']), true);
$iv = base64_decode($payload['iv']);
$key = base64_decode(substr($base64_key, 7));
$sessionId = openssl_decrypt($payload['value'], 'AES-256-CBC', $key, 0, $iv);
echo "Session Id: $sessionId <br/>";
$app['session']->driver()->setId($sessionId);
$app['session']->driver()->start();
print_r($app['session']->driver()->get('user'));exit();
}catch (Exception $e){
print_r($e->getMessage());exit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment