Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make .PHP file only accessible to the server?

I created a cron job through goDaddy control center.

The cron job is in the folder "cron jobs".

I don't want anyone to be able to run it, how should I set the permissions of the folder so that it can't be publicly opened but it still can be used for the cron job?

Will unchecking Public > Read be enough to prevent anyone from running it?

like image 296
lisovaccaro Avatar asked Nov 18 '11 21:11

lisovaccaro


3 Answers

Just put the files outside of the webroot/document root folder.

like image 180
GolezTrol Avatar answered Sep 27 '22 19:09

GolezTrol


In .htaccess add this.

<Location /cronjobs>
order deny,allow
deny from all
allow from 127.0.0.1
</Location>

I included allow from 127.0.0.1 so it can be run from the server, i.e. so the cron can still run.

like image 30
danielrsmith Avatar answered Sep 27 '22 18:09

danielrsmith


Another possible solution if the file is meant to be used exclusively as an include() and not ran standalone by a user who enters it in the url.

Place this code at the top of the file you want to block direct calling of.

if(basename($_SERVER['PHP_SELF']) == 'blockedFile.php')
    {
    header('Location: ./index.php');
    exit();
    }

PHP checks if the file's name is the one being ran directly. If blockedFile.php were included in index.php with include() then basename($_SERVER['PHP_SELF']) would equal index.php. If it were standalone, it would equal blockedFile.php and send the user back to the index page.

like image 29
Ryan Mortensen Avatar answered Sep 27 '22 17:09

Ryan Mortensen