Support the ongoing development of Laravel.io →
Requests Session Packages

I want to use an external PHP library with Laravel 5. The problem is that I need a way to pass a few variables to the external PHP's config file. I've tried setting a session variable, but I can't read the Laravel session values in the external library. I then considered using cookies, but since cookies are encrypted, I can't access those either. Any suggestions on how I can achieve this?

Last updated 3 years ago.
0

Have you tryed regular sessions for this task only?

0

Yes, I have tried to set a $_SESSION variable within Laravel and read it back in the external PHP, but that didn't work. The external PHP is Responsive FileManager. I'm trying to pass a Laravel username to it so I can have it set a home directory path for each user as in the documented $_SESSION["RF"]["subfolder"] ="subfolder/name/"

Any ideas?

0

$_GLOBAL?

0

Thanks @slayerfat for the suggestion, but I tried setting both $_GLOBAL['my_var'] and $GLOBALS['my_var'] without any success. I am still unable to access my variable from outside of Laravel.

Surely, there must be a way to read the variable. Am I overlooking something?

0

Ok, after toying around I found that the session path was not on my web root. Once I set the path to "/", I was able to read the session variable outside of Laravel. Here is my example

/*
  Set regular PHP cookie from within Laravel
  Notice the fourth parameter set to /
*/
setcookie("current_user", Auth::user()->username,0,"/");

Now on my external file I can access the variable as follows:

// Read cookie as expected:
echo $_COOKIE['current_user']

This seems to work, but I will test a little bit more. There may be a better, more secure way. If anyone has a better suggestion, please let me know!

Update: I recommend you add a little bit of encryption/decryption when storing/retrieving cookie data and this works well.. I will mark it as a solution in hopes that it helps others.

Last updated 10 years ago.
0

use:

<?php session_start(); $_SESSION["RF"]["subfolder"] = "folder_user1" //subfolder of your user ?>
0

It's 2016, but for anyone having this problem:

This might help you out. :)

[https://bgtsworks.blogspot.com/2016/08/access-laravel-session-outside-laravel.html]

Last updated 8 years ago.
0

djtechonline said:

Ok, after toying around I found that the session path was not on my web root. Once I set the path to "/", I was able to read the session variable outside of Laravel. Here is my example

/*
 Set regular PHP cookie from within Laravel
 Notice the fourth parameter set to /
*/
setcookie("current_user", Auth::user()->username,0,"/");

Now on my external file I can access the variable as follows:

// Read cookie as expected:
echo $_COOKIE['current_user']

This seems to work, but I will test a little bit more. There may be a better, more secure way. If anyone has a better suggestion, please let me know!

Update: I recommend you add a little bit of encryption/decryption when storing/retrieving cookie data and this works well.. I will mark it as a solution in hopes that it helps others.>djtechonline said:

Ok, after toying around I found that the session path was not on my web root. Once I set the path to "/", I was able to read the session variable outside of Laravel. Here is my example

/*
 Set regular PHP cookie from within Laravel
 Notice the fourth parameter set to /
*/
setcookie("current_user", Auth::user()->username,0,"/");

Now on my external file I can access the variable as follows:

// Read cookie as expected:
echo $_COOKIE['current_user']

This seems to work, but I will test a little bit more. There may be a better, more secure way. If anyone has a better suggestion, please let me know!

Update: I recommend you add a little bit of encryption/decryption when storing/retrieving cookie data and this works well.. I will mark it as a solution in hopes that it helps others.

Appreciate your work djtechonline

Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.