PHP: prevent direct access to page

跟風遠走 提交于 2019-11-29 15:22:29

Don’t redirect but send the 404 status code:

header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
exit;

for the search engines, if you return HTTP status 404 they should not index I believe. But you could always redirect to somewhere covered by a robots.txt

Just to clarify:

  • You have some PHP that you want available to other PHP programs on the system
  • You do not want anybody accessing it except by running one of the other PHP programs

(i.e. "direct" doesn't mean "except by following a link from another page on this site")

Just keep the PHP file outside the webroot. That way it won't have a URL in the first place.

James

To ensure Search Engines don't index it, use a header() command to send a 404 lke this;

header("HTTP/1.0 404 Not Found");

Or put all such files in one folder, "includes" say, and add a "Deny /includes/" into your robots.txt file. This way, you can also add a ".htaccess" file in the same directory with one line - "Deny From All" - this will tell Apache to block access (if apache is configured properly), for another layer of security.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!