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

mchulet's avatar

Protect access to PHP file?

Hi,

One of the features that I want to provide in my laravel application is the ability to allow the user to execute pre-built PHP scripts only if they are an authenticated user. I can place these legacy PHP scripts anywhere in the laravel application (e.g. public or storage folder), but I cannot rewrite them in Laravel.

e.g. if the user access http://localhost/php/test.php, then my laravel application should show the test.php output only if he is an authenticated user.

Can this be done? Any help is highly appreciated. Thanks in advance!

0 likes
6 replies
ohffs's avatar
ohffs
Best Answer
Level 50

I guess you could have a route something like :

Route::get('/php/{script}', ['middleware' => 'auth', function ($script) {
    require_once(storage_path("/legacy_php_scripts/{$script}"));
}]);

Nasty though... :-/

mchulet's avatar

Works perfectly well! Thank you for the prompt reply.

bashy's avatar

Be VERY careful about that.

Depending on how you code it and your PHP settings, someone could include a system file or something by going up levels. OR even include a php file via a URL. php/../logs/laravel.log could be used to find your current directory and then they could perform another one to grab any system file.

pmall's avatar

Yes you have to sanitize the path provided in the url.

ohffs's avatar

@bashy @pmall I was kind of assuming that was taken as read - I forget sometimes that people might not think of that kind of thing. Although given I've just found over 100 methods in the bit of code I've inherited that pass un-escaped user input to raw SQL queries... fun times...

Please or to participate in this conversation.