How to add one line to all php files?

So good. I have many php files and one index.php file. All files cannot work without index.php file, because I include them in index.php. For instance. if someone clicked Contact us, the url will become the same as index.php?id=contact, and I use $_GET['id']to include the contacts.php file. But if someone finds a file path, for example /system/files/contacts.php, I do not want this file to be executed. So, I realized that I can add before including any files in the index.php line, like this one $check_hacker = 1, and use it ifin every file starting like if($check_hacker <> 1) die();. So how can I do this without opening all the files and adding this line to each of them? Is it possible? Because I actually have a lot of .php files. And maybe there is another way to turn off viewing a single file? Any ideas?
Thank.

+3
source share
7 answers

You can put your index.php in your web directory. And put all the files that it includes in another non-web directory.

Say you are at http://www.example.com/index.php actually /path/to/your/home/www/index.php, you can put contact.php in /path/to/your/home/includes/contact.php. No .htaccess, rewrite, auto add. Just a good file structure and server configured as needed.

Edit the detailed comment for using xamp:

In your httpd.conf file, add something like this:

<Directory "/path/to/your/site/root">
    Options Indexes FollowSymLinks
    AllowOverride all
    Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1
</Directory>


<VirtualHost *:80>
    DocumentRoot /path/to/your/site/root
    ServerName www.example.org
</VirtualHost>

Then in your Windows hosts file (in C: \ Windows \ System32 \ drivers \ etc) add this line:

127.0.0.1   www.example.com
+10
source

.htaccess, , index.php, , .

( ), css, js ..:

order deny,allow
<FilesMatch "\.php">
    deny from all
</FilesMatch>
<FilesMatch "(index.php)">
    allow from all
</FilesMatch>

- , . : .php, index.php

, FilesMatch php index.php.

EDIT: , , .

+9

Kau-Boy:

php ( index.php) .htaccess :

deny from all

, /css/jscript , .

+5

mod_rewrite ( Apache). , gazillions if PHP.

, - " " /system/files/contacts.php, index.php?id=contact .

+4

php.ini htaccess :

auto_prepend_file="[path to some .php file]"

, php .

php.ini auto_append_file , PHP .

+3
RewriteCond %{REQUEST_URI} system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

Any attempt to return the system folder to the root directory will be redirected!

+1
source

Source: https://habr.com/ru/post/1747861/


All Articles