最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - How to prevent access to certain URL requested pages? - Stack Overflow

matteradmin7PV0评论

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?

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?

Share Improve this question asked Jun 6, 2009 at 21:09 BabikerBabiker 18.8k28 gold badges82 silver badges127 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 10

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.

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 remended. 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.

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/docs/2.2/mod/core.html#files

Example .htaccess:

<Files processFrom.php>
Order allow,deny
Deny from all
</Files>
Post a comment

comment list (0)

  1. No comments so far