Jump to content

prevent access of directory except from certain files with .htaccess?


kaiman

Recommended Posts

Hi Everyone,

 

I have the following script written in PHP that is supposed to stop people from directly accessing a certain directory unless they come from a particular page (contactform.php in this example which is a form processing script that uses header: Location to redirect to the error and success pages).

 

Of course it is falling victim to the fact that most modern browsers  (such as Firefox) don't send HTTP_REFERER information and the variable is left blank. My question is is there a way to do this using an .htaccess file on an Apache WS to bypass the browser altogether. What would something like this look like?

 

Thanks for the help,

 

kaiman

 

PHP Code:

 

<?
$referrer = $_SERVER['HTTP_REFERER'];
// set page that it is okay to view from
if (preg_match("http://www.mydomain.com/scripts/php/contactform.php",$referrer)) {
      header('Location: http://www.mydomain.com/contact/error/');
}
// otherwise redirect to contact page 
else {
      header('Location: http://www.mydomain.com/contact/');
};
?> 

Link to comment
Share on other sites

You can't bypass the browser :-\ It's the browser that's making the request to your server in the first place.

 

Your only options are:

- Restrict by referrer. As you know it's not always present, and it's easily forged

- Restrict by IP. Better, but a pain to manage

- Restrict by credentials (eg, username and password). Best

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.