Jump to content

Protecting Included Files


jdrawmer

Recommended Posts

Hi All,

 

I'm trying to secure my web app which is currently in development, and came across this issue.

 

I have a header.php and footer.php page which are included to every page, with the content in the middle. The problem is, if you visit header.php then it displays the header, with some blank text.

 

What is the best way to protect this - i.e., if visited directly, it re-directs to index.php etc.

 

My initial thought is to set a $happylink on each page and in the header and footer, checking basically doing the following

 

if (isset($happylink) && !empty($happylink))
{
     blah blah;
}
else
{
     Header("Location: index.php");
}

 

Would that be the best way? Is there something easier?

Link to comment
Share on other sites

Files that aren't designed to be accessed directly should be stored outside of your servers document root and there location should be added to your include path. failing the ability to do so (shity shared hosting) you can define a constant within the file calling the include, then check for the existence of that constant within the include itself. eg;

 

<?php

define('IS_WITHIN_APP', true);

include 'foo.php';

// more code

 

foo.php

<?php

if (!defined('IS_WITHIN_APP')) {
  die('Bugger Off!');
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.