views:

180

answers:

3

How to prevent access to certain URL requested pages?

If i have form.html, processFrom.php and getResults.php in my webapp root, even though processFrom.php does not echo any content, how can i prevent the user from accessing this file by typing in the URL?

+10  A: 

Presumably you only ever access it via an include statement, or similar? Your safest bet would be to put it elsewhere on your filesystem, and include it from there.

Anything that isn't served by the web server shouldn't be kept under the web root.

RichieHindle
A: 

When you work with an Apache web server I would suggest to create a .htaccess file and deny the access the file.

These links should help you setup a .htaccess file of your needs.

http://httpd.apache.org/docs/2.2/mod/core.html#files

Example .htaccess:

<Files processFrom.php>
Order allow,deny
Deny from all
</Files>
166_MMX
+2  A: 

The existing answers assume that processForm.php contains only code, and is not the page specified in the "action" attribute of your form. In case this assumption is incorrect, I'll answer assuming you DO want this page to directly process the POST request generated by your form, but that you want to prevent anybody from accidentally or maliciously running code in this file.

In this case, you can't hide the file as recommended. Instead, you could use a token that is created when the form is displayed, stored in a session variable, and also submitted with the form (<input type="hidden" ... />). Before doing any processing in processForm.php, you check that the token is present and matches the one in the session variable. Also, you should always sanitize all form input. There's no stopping somebody submitting whatever they want to your script as long as it's web accessible.

grossvogel