Skip to main content

Deny direct access to all .php files except index.php


I want to deny direct access to all .php files except one: index.php



The only access to the other .php files should be through php include .



If possible I want all files in same folder.



UPDATE:



A general rule would be nice, so I don't need to go through all files. The risk is that I forget a file or line.



UPDATE 2:



The index.php is in a folder www.myadress.com/myfolder/index.php



I want to deny access to all .php files in myfolder and subfolder to that folder.


Source: Tips4allCCNA FINAL EXAM

Comments

  1. In index.php, add an access value like this:

    $access = 'my_value';


    In every other file, include this check before even a single byte is echoed out by php:

    if(empty($access)) {
    header("location:index.php");
    die();
    }


    This way, other php files will be accessible only through require / include and not through the url.

    ReplyDelete
  2. Are you sure, you want to do that? Even css and js files and images and ...?

    OK, first check if mod_access in installed to apache, then add the following to your .htaccess:

    Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1

    <Files /index.php>
    Order Allow,Deny
    Allow from all
    </Files>


    The first directive forbids access to any files except from localhost, because of Order Deny,Allow, Allow gets applied later, the second directive only affects index.php.

    Caveat: No space after the comma in the Order line.

    [EDIT:]

    To allow access to files matching *.css or *.js use this directive:

    <FilesMatch "*\.(css|js)$">
    Order Allow,Deny
    Allow from all
    </FilesMatch>


    You cannot use directives for <Location> or <Directory> inside .htaccess files, though.

    Your option would be to use <FilesMatch "*\.php$"> around the first allow,deny group and then explicitely allow access to index.php.

    ReplyDelete
  3. You can try defining a constant in index.php and add something like

    if (!defined("YOUR_CONSTANT")) die('No direct access');


    to the beginning of the other files.

    OR, you can use modrewrite and redirect requests to index.php, editing .htaccess like this:

    RewriteEngine on
    RewriteCond $1 !^(index\.php)
    RewriteRule ^(.*)$ /index.php/$1 [L]


    Then you should be able to analyze all incoming requests in the index.php and take according actions.

    If you want to leave out all *.jpg, *.gif, *.css and *.png files, for example, then you should edit second line like this:

    RewriteCond $1 !^(index\.php|*\.jpg|*\.gif|*\.css|*\.png)

    ReplyDelete
  4. An oblique answer to the question is to write all the code as classes, apart from the index.php files, which are then the only points of entry. PHP files that contain classes will not cause anything to happen, even if they are invoked directly through Apache.

    A direct answer is to include the following in .htaccess:

    <FilesMatch "\.php$">
    Order Allow,Deny
    Deny from all
    </FilesMatch>
    <FilesMatch "index[0-9]?\.php$">
    Order Allow,Deny
    Allow from all
    </FilesMatch>


    This will allow any file like index.php, index2.php etc to be accessed, but will refuse access of any kind to other .php files. It will not affect other file types.

    ReplyDelete
  5. How about keeping all .php-files except for index.php above the web root? No need for any rewrite rules or programmatic kludges.

    Adding the includes-folder to your include path will then help to keep things simple, no need to use absolute paths etc.

    ReplyDelete
  6. An easy solution is to rename all non-index.php files to .inc, then deny access to *.inc files. I use this in a lot of my projects and it works perfectly fine.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex