0

So I'm trying to make a theme from my Html file. And using require('file.php') where file.php is a series of different components of my theme. Now when I access the file directly, I still see the html. How do I make it display a blank page when user accesses the file.php directly?

Explanation So let's say I make index.php and I want to include the header file(header.php)

When I require('header.php'), everything works perfect. Now when I try to access the header.php, I can see the html content of it. How to I make this appear blank instead of seeing the header.php piece? Thanks

Maz I
  • 3,664
  • 2
  • 23
  • 38
Please Delete me
  • 807
  • 2
  • 10
  • 15

4 Answers4

2

you can use $_SERVER['SCRIPT_FILENAME'] here to check and then make page blank in header.php.

<?php
if($_SERVER['SCRIPT_FILENAME'] == 'header.php') {
    exit();
}
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
2

In that case, If you want to execute header.php inside the index.php or etc. You need to add or define a flag in parent files (wherever you want header.php to be executed or simply adding in to the common file which is called in all parent files), In header.php file you need check the defined flag is set or has some value. If it is not set, then stop execution by using die(); function.

In index.php,

<?php $HeaderAllow ='1';  ?>

In header file,

 <?php 
     if($HeaderAllow=='' or !isset($HeaderAllow)){
         die();
     }
 ?>
Krish R
  • 22,583
  • 7
  • 50
  • 59
1

Putting the include files in a seperate folder and protectinng with .htaccess is good idea.

Same issue here.

deny direct access to a folder and file by htaccess

Community
  • 1
  • 1
Maz I
  • 3,664
  • 2
  • 23
  • 38
  • - Maz - Denying access is one thing, but what if I just want the page to appear blank? – Please Delete me Nov 14 '13 at 12:57
  • then you can do like have some variable set in index file. and check that variable in header.php. If someone accesses this fiel directly variable will not be set so you can simply echo die() or exit. in top part of page. see the above code snippet by @Chinnu R – Maz I Nov 14 '13 at 13:05
0

The easiest way is to put your includes in a directory and deny access to that directory in your .htaccess file

user42701
  • 11
  • 7