Add Require Statement To All PHP Pages Using htaccess?


#1

Hey,

I am wondering how I can add a require statement to all php pages using htaccess or any other method.

Seeing as at the moment I am having to add the require statement to every php page. :frowning:

Here it is:

<?php require("/home/war59312/willsdownloads.com/ban/ban.php"); ?>

Here is my current htaccess file:

ErrorDocument 401 /errors/401.php
ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(www.)?willsdownloads.com(/)?.$ [NC]
RewriteRule .
.(gif|jp?g|bmp|png|cab|7z|cmd|txt|rar|zip|exe|ex_|dll|dl_|inf|in_|sys|sy_)$ - [F,NC]

RewriteCond %{HTTP_USER_AGENT} Wget [OR]
RewriteCond %{HTTP_USER_AGENT} CherryPickerSE [OR]
RewriteCond %{HTTP_USER_AGENT} CherryPickerElite [OR]
RewriteCond %{HTTP_USER_AGENT} EmailCollector [OR]
RewriteCond %{HTTP_USER_AGENT} EmailSiphon [OR]
RewriteCond %{HTTP_USER_AGENT} EmailWolf [OR]
RewriteCond %{HTTP_USER_AGENT} ExtractorPro
RewriteRule ^.*$ X.html [L]

Rewritecond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule .php$ /errors/404.php

Options -Indexes
AddType application/cab .cab .sy_ .dl_ .ex_ .in_ .ch_
AddType application/cab cab sy_ dl_ ex_ in_ ch_
AddType application/x-httpd-php .foo

Thanks a lot,
Will

http://www.willsdownloads.com/


#2

[quote]<?php require("/home/war59312/willsdownloads.com/ban/ban.php"); ?>[/quote]
I am not aware of any method you can use that will stick that line onto every page, but you can make it a bit more useful and portable. Instead of including ban.php, include a generic config.php (put it in an “includes” directory) instead. You will then be able to include ban.php in the new file, plus any others that you may decide to add at a later date.

Now the cool bit. If you move servers, you’ll be forced to edit the path of your require function on every page, because you are currently using a relative-from-root path. Instead, put this code at the beginning of all your pages, which makes use of a special environment variable:

<?php $path = $_SERVER['DOCUMENT_ROOT']; require("$path/includes/config.php"); ?>If you think you might be moving your site to an IIS webserver, $_SERVER[‘DOCUMENT_ROOT’] may not always be available. It will be if that environment variable has been specifically create, or if PHP has been installed as an ISAPI module.

One more thing. require() produces a fatal error, if an error occurs. include() just produces an error message.


Simon Jessey
Keystone Websites | si-blog


#3

Try auto_prepend_file in your .htaccess file:

php_value auto_prepend_file /path/to/file.php Note that this requires running PHP as an Apache module, not as CGI.


If you want useful replies, ask smart questions.


#4

One more thing. require() produces a fatal error, if an error occurs. include() just produces an error message.

Ideally, you don’t want either one shown to your readers, as it will reveal your filesystem paths. Using an “@” before most functions will supress any error output if they fail:

@include_once "file.php"; It’s generally a good practice to disable error output globally. In .htaccess:

php_flag display_errors off php_flag log_errors on This will cause your errors to be sent to your error log, not to the client.

Now the cool bit. If you move servers, you’ll be forced to edit the path of your require function on every page, because you are currently using a relative-from-root path. Instead, put this code at the beginning of all your pages, which makes use of a special environment variable:

Even simpler is to simply not use a path in your include() or require() statements at all, and just set your PHP include path in .htaccess with the directory you keep that stuff in:

php_value include_path ".:/path/to/your/include/directory:/path/to/secondary/directory" This way you can include files in multiple directories without having to type out full paths for any of them.

@include “note_about_apache_module_not_cgi_etc”;


If you want useful replies, ask smart questions.


#5

All right thanks a lot both of you. :slight_smile:

I figured it out.

Thanks again,
Will

http://www.willsdownloads.com/