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

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

我正在尝试找出如何防止直接访问处理程序文件,而不通过阻止对处理程序的访问来破坏表单。如果说得通?

表格

        System.out.println("The letter at position " + i + " is " + showCharacter(str,i));

处理程序

<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>
hubeihb 回答:防止直接访问PHP表单处理程序

添加

if(isset($_POST['first-name'],$_POST['last-name'],$_POST['email'])){
   //Do rest of page...
}else{
   //Do nothing or throw error
}

在脚本开头检查是否已输入

,

通过使用超全局$ _SERVER,我找到了适合我的解决方案。通过此操作,我能够检测到REQUEST_METHOD,在我的情况下为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;
本文链接:https://www.f2er.com/3169755.html

大家都在问