Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lat4732's avatar
Level 12

Using the Auth facade inside a custom PHP file

Hello.

How can I use the Auth facade inside a custom PHP file? I need to catch user's administration level (Auth::user()->level) and based on that do some business logic inside that PHP file.

0 likes
21 replies
Sinnbeck's avatar

What do you mean by a custom PHP file? Just a foo.php in your public directory that you want to call?

Sinnbeck's avatar

@Laralex You would need to boot laravel completely as the session is encrypted using your APP_KEY. So it will take some work getting it working correctly. You might be able to load composer autoload file as well as /bootstrap/app.php

But the question is why. This is going to be a pain to build and maintain.

lat4732's avatar
Level 12

@Sinnbeck Because I need to restrict specific users from a specific permissions.

array(
		'read' => true, 
		'write' => (Auth::user()->level > 1) ? true : false, 
		'locked'=> true
)

This php file is accessed through AJAX. Maybe send as data an encrypted information and then catch it inside that file?

Sinnbeck's avatar

@Laralex Implement it yourself. It seems to mostly be these two lines (you just need to make the config)

$connector = new elFinderConnector(new elFinder($opts));
$connector->run();
lat4732's avatar
Level 12

@Sinnbeck And what about encrypting and passing the user's level through the AJAX request and then catching it inside the php file?

lat4732's avatar
Level 12

@nebulous75 Not moving the code into a Laravel route.

@sinnbeck I already built everything to work without package and it will be really painful to start from the beginning. I'll try the data pass through AJAX solution and I'll reply back. Any suggestions on encrypting the data?

lat4732's avatar
Level 12

@Sinnbeck Okay. I did it really simple as this page is only usable from administrators which won't do anything harmful.

=================

Added to the AJAX request a new data called htr5 (just a random name) which has the following contents:

8475{{ auth()->user()->level }}9785

Starts and ends with static numbers which are excluded when the data arrives so I end up with the user level. I don't think encryption is needed at this point.

uloncl's avatar

add

use Illuminate\Support\Facades\Auth;
lat4732's avatar
Level 12

@nebulous75 Tried that but doesn't seems to work.

use Illuminate\Support\Facades\Auth;

return json_encode(Auth::user()->level);

this returns

Fatal error: Uncaught Error: Class "Illuminate\Support\Facades\Auth" not found
Snapey's avatar

its not going to work. You have access to nothing if you don't boot the framework

what possible reason is there for a standalone php script?

Snapey's avatar

@Laralex you have started down a flawed strategy and now have to much invested in it to back out?

Sinnbeck's avatar

@mahsanr44 What really? You implemented that php code in a separate file as well?! :O

1 like

Please or to participate in this conversation.