Page 1 of 1

How to disable direct access to a file

Posted: Sat Oct 24, 2009 1:26 am
by techkid
Suppose I've 2 Files. 1.php & 2.php

I don't want anybody to access 2.php directly from browser. Eg: http://localhost/2.php but I want 2.php show up in 1.php like:

Code: Select all

<?php
include("2.php");
?>
My question is how can I disable direct access using php not .htaccess

Re: How to disable direct access to a file

Posted: Sat Oct 24, 2009 1:34 am
by guosheng1987
maybe you can wirte code like these

Code: Select all

 
1.php
 
<?php
$a = 5;
include ("2.php");
?>
 
 
2.php
 
 
<?php
if(empty($a))
 redirect...
?>
 
 

Re: How to disable direct access to a file

Posted: Sat Oct 24, 2009 2:24 am
by requinix
If you don't want 2.php accessible from the web then put it in a place that's not accessible from the web.

Re: How to disable direct access to a file

Posted: Sat Oct 24, 2009 3:17 am
by techkid
guosheng1987 wrote:maybe you can wirte code like these

Code: Select all

 
1.php
 
<?php
$a = 5;
include ("2.php");
?>
 
 
2.php
 
 
<?php
if(empty($a))
 redirect...
?>
 
 
Any other possible ways?

Re: How to disable direct access to a file

Posted: Sat Oct 24, 2009 3:42 am
by Naeem
You can also restrict access to your files by doing this:

Code: Select all

 
if (preg_match("/filename.php/", $_SERVER['SCRIPT_FILENAME'])) {
    die("Restricted area get out of here");
    exit;
}
 
change filename.php with your php file name
Hope this help.
Thanks.