我的网站上有一个名为 “check.php” 的 php 文件,它会在提交表单时执行。
假设我的网站是 "myweb.com" 并且 php 文件在目录 "PHP"中
我想阻止对“check.php”文件的直接 url 访问,即如果有人键入 url“myweb.com/PHP/check.php”,则不应执行 php 文件,而应返回一条错误消息。
我试图通过在 .htaccess 中设置规则来阻止访问,但即使我尝试提交表单,它也会阻止 php。
.htaccess 规则:
RewriteEngine on
RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/
(.*)\.php$ /index.html [L]
有什么办法可以做到吗?
最佳答案
你可以用 PHP 来做
<?php
/* at the top of 'check.php' */
if ( $_SERVER['REQUEST_METHOD']=='GET' && realpath(__FILE__) == realpath( $_SERVER['SCRIPT_FILENAME'] ) ) {
/*
Up to you which header to send, some prefer 404 even if
the files does exist for security
*/
header( 'HTTP/1.0 403 Forbidden', TRUE, 403 );
/* choose the appropriate page to redirect users */
die( header( 'location: /error.php' ) );
}
?>
关于php - 防止直接 url 访问 php 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33999475/