user3464859
user3464859

Reputation: 15

Include a php file in htaccess and run it on other php files

How can I include a php file in htaccess to work on all other php files first?
For example here are the files in my web root:

index3.php
index4.php
check.php
folder1
folder2

When I execute a php file I would like the check.php file to run first.

Upvotes: 0

Views: 1244

Answers (2)

Cyclonecode
Cyclonecode

Reputation: 29991

You could use sort of a gateway file for this, at the bottom or top of your check.php file you add a include directive like:

// here you need to check if the file exists and are a valid php file etc
$file = ((isset($_GET['file']) && is_file($_GET['file'])) ? $_GET['file'] : null);
if(is_file($file)):
  // include the file
  include_once($file);
endif;

Then in your .htaccess file you add something like:

// route every request to 'check.php' appending the filename of the request
// of course you also would have to check so the request isn't a folder or invalid
// file like an image css etc
RewriteRule (.*) check.php?file=$1 [L,QSA]

I'm not sure how safe the above is but it should work.

Upvotes: 0

Tularis
Tularis

Reputation: 1506

You can add the following line to your .htaccess file to (automatically) include a file before the requested file runs. Like this:

php_value auto_prepend_file "file.php"

More info at the PHP documentation auto_prepend_file and php_value in htaccess

Upvotes: 2

Related Questions