-1

I am a newbie to PHP, I would like to create a prevent direct access to a php include file with .htaccess page

DemoSite/    <-folder
        index.php
        global_variable.php
        .htaccess

I want to prevent direct access global_variable.php file

like if ( $_SERVER['REQUEST_METHOD']=='GET' && realpath(__FILE__) == realpath( ...)

Anyidea how to use .htaccess to achieve it???

Thank you very much

PeterOcean
  • 15
  • 3

2 Answers2

1

Just add a string Deny from all in .htaccess file to your include directory

Or

If you want to prevent a file Then use

<Files "global_variable.php">  
  Order Allow,Deny
  Deny from all
</Files>
0

The simple practice is adding file to a folder (so the rules just apply to the desired file in folder) and surrounding the folder with this htaccess :

Deny from all
allow from 127.0.0.1
allow from YOUR_SERVER_IP

But a better way with php would be to define a value in your index file or any file that is gonna get accessed by user and include or require your desired file, then in it you can check if a value is defined or not, Example :

index.php

define('ACCESS', true);

global_variable.php

defined('ACCESS') or die("No direct access is allowed");
Erfan Mola
  • 111
  • 1
  • 11