Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
防止直接访问PHP表单处理程序_Php_Wordpress_Forms - Fatal编程技术网

防止直接访问PHP表单处理程序

防止直接访问PHP表单处理程序,php,wordpress,forms,Php,Wordpress,Forms,我在我的网站(Wordpress)上使用了许多表单,它们使用method=“post”将PHP文件用作表单上的“操作”。所有这些都很好,但是我收到了大量空白的表单提交,这是在直接访问PHP处理程序时发生的(因为直接访问处理程序文件绕过了我的前端表单验证,并且处理程序包含发送电子邮件的wp_mail函数) 我正在尝试解决如何阻止直接访问处理程序文件,而不通过阻止访问处理程序破坏表单。这有意义吗 表格 <form class="contact-form" action="<?=get_b

我在我的网站(Wordpress)上使用了许多表单,它们使用method=“post”将PHP文件用作表单上的“操作”。所有这些都很好,但是我收到了大量空白的表单提交,这是在直接访问PHP处理程序时发生的(因为直接访问处理程序文件绕过了我的前端表单验证,并且处理程序包含发送电子邮件的wp_mail函数)

我正在尝试解决如何阻止直接访问处理程序文件,而不通过阻止访问处理程序破坏表单。这有意义吗

表格

<form class="contact-form" action="<?=get_bloginfo('template_url') . '/path/to/my/handler-file.php' ?>" method="post" enctype="multipart/form-data">
    <input type="text" id="first_name" name="first_name" placeholder="First name">
    <label for="first_name">First name</label>

    <input type="text" id="last_name" name="last_name" placeholder="Last name">
    <label for="last_name">Last name</label>

    <input type="email" id="email" name="email" placeholder="Your email">
    <label for="email">Your email</label>

    <button class="button" type="submit">Submit</button>
</form>
添加


到脚本的开头,检查是否输入了这些内容

我找到了一个适合我的解决方案,使用superglobal$\u服务器。由此,我能够检测到REQUEST_方法,在我的例子中,它将是POST,正如在我的HTML中设置的那样

// Only run if request uses POST method (as set in <form> html)
// This helps to avoid blank submissions, as accessing the file directly will not run
if ( $_SERVER["REQUEST_METHOD"] == "POST" ) :

    // My handling code here

// If request does not use POST method, display error
else :
   echo 'Direct access to this file is prohibited.';
endif;
//仅当请求使用POST方法时运行(如html中设置的)
//这有助于避免空白提交,因为直接访问文件将不会运行
如果($\u服务器[“请求\u方法”]=“发布”):
//我的处理代码在这里
//如果请求未使用POST方法,则显示错误
其他:
echo“禁止直接访问此文件”;
endif;

如果没有验证码,您无法真正阻止直接访问。但是,检查以确保存在预期的变量将防止您收到空白电子邮件。您好@moshe,我可以改为检查$\u服务器请求方法吗?i、 e如果($_服务器[“请求方法”]=“发布”){}否则{}endif;
if(isset($_POST['first-name'], $_POST['last-name'], $_POST['email'])){
   //Do rest of page...
}else{
   //Do nothing or throw error
}
// Only run if request uses POST method (as set in <form> html)
// This helps to avoid blank submissions, as accessing the file directly will not run
if ( $_SERVER["REQUEST_METHOD"] == "POST" ) :

    // My handling code here

// If request does not use POST method, display error
else :
   echo 'Direct access to this file is prohibited.';
endif;