Zend - The PHP Company




Security

Add Code


protect php include file  

Type: application
Added by: imamferianto
Entered: 31/05/2003
Last modified: 09/12/2002
Rating: - (fewer than 3 votes)
Views: 7692
This is describe how we can protect php include file unser directory that not protected, this is simply done using php function


<?php
/* syntax
   adding this line on top your script code
   before outpur anything
   to prevent this file when not include

  sytax 1 :

  if(!eregi("THIS_FILE_NAME",implode(";",get_included_files()))){ exit;}

 THIS_FILE_NAME= replace with the include 
 file name you want to protect 
{must be this file  name valid}

 note: do not use $PHP_SELF for above 
 script because it does not same with
 this_file_name when its include

syntax 2:
 
 alternative script using $PHP_SELF is :

 if($PHP_SELF!="main_file_name") exit;
 
 you can replace main_file_name with
 file that will calling this include file but
 it is not efective when multiple file
 want to calling this file in include function

 I suggest using syntax 1 is the best

 
*/


//example if this filename is config.php
//include handler
if(!eregi("config.php",implode(";",get_included_files()))){ exit;}


///your code goes here
//..................
//.................

?>


Usage Example




Rate This Script





Search



This Category All Categories