Question

I would honestly be surprised if anything like this exists, but I couldn't find anything relevant with my Google-fu.

I'm including a PHP file using require_once() on every page of my site. The file is a light custom-made framework.

I was wondering if I could drop the entire require_once() line and, by using another obscure mechanism, include the library at the beginning of every file in a specified folder.

The reason behind this is that the files in that particular folder are going to be modified by people with limited coding knowledge and I don't want them screwing up the PHP lines thinking they're not important.

EDIT: The library will be ultimately distributed and aims to work on typical PHP installatons.

Thanks!

Was it helpful?

Solution

Another technique that I feel is less obscure than auto_prepend_file would be to have all requests to this folder first redirected to one file, that then loads the library, and then loads the file you're going to have these other users modifying.

Here's how I've parsed this sort of thing using your rewrite code... If the htaccess file is in the same directory as the folder, I think the rewrite rule will look like this: RewriteRule ^(.*)$ index.php/$1 [L]

In the index.php file I usually load some sort of configuration for the site, followed by delegating. I use this function right here to get the current url... I'm not entirely sure if it's overkill or not, but it's worked consistently for me across several platforms.

function get_current_url() {
 $pageUrl = 'http';
 if (@$_SERVER["HTTPS"] == "on") {$pageUrl .= "s";}
 $pageUrl .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageUrl .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageUrl .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }

 return $pageUrl;
}

Then you can essentially parse the result of get_current_url() using php's parse_url() function and delegating appropriately.

OTHER TIPS

Yes, this is very possible, using a php.ini directive auto_prepend_file.

So, for instance, in the appropriate .htaccess file:

php_flag auto_prepend_file header.php
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top