javascript - 检查表单的安全性。

标签 javascript php forms security spam

我的帐户因垃圾邮件多次被暂停,我的主机提供商告诉我检查我的网站安全性。可能是我的表格不够安全。您认为此表单可用于发送垃圾邮件吗?

这是我的代码:

<script type="text/javascript">
$(document).ready(function () {
    $('#form').ajaxForm({
        beforeSubmit: validate
    });

    function validate(formData, jqForm, options) {
        var name = $('input[name=name]').fieldValue();
        var email = $('input[name=email]').fieldValue();
        var company = $('input[name=company]').fieldValue();
        var location = $('input[name=location]').fieldValue();
        var phone = $('input[name=phone]').fieldValue();
        var message = $('textarea[name=message]').fieldValue();

        if (!name[0]) {
            alert('Please enter your name');
            return false;
        }
        if (!company[0]) {
            alert('Please enter the name of your organization');
            return false;
        }
        if (!email[0]) {
            alert('Please enter your e-mail address');
            return false;
        }
        if (!phone[0]) {
            alert('Please enter your phone number');
            return false;
        }
        if (!location[0]) {
            alert('Please enter your location');
            return false;
        }
        if (!message[0]) {
            alert('Please enter your message');
            return false;
        }

        else {

        $("#form").fadeOut(1000, function () {
            $(this).html("<img src='note.png' style='position: relative;margin: 0 auto;width: 500px;left: 20px;top: 30px;'/>").fadeIn(2000);
        });

        var message = $('textarea[name=message]').val('');
        var name = $('input[name=name]').val('');
        var email = $('input[name=email]').val('');
        var phone = $('input[name=phone]').val('');
        var company = $('input[name=company]').val('');
        var location = $('input[name=location]').val('');

            } 
    }

});

</script>

html:

<form id="form" method="post" name="form" action="send.php">

<input id="name" type="text" name="name"/>

<input id="company" type="text" name="company"/>

<input id="email" type="text" name="email"/>

<input id="phone" type="text" name="phone"/>

<input id="location" type="text" name="location"/>

<textarea name="message" id="message" rows="10"></textarea>

<input class="submit" type="submit" value="send" name="submit"></input>

</form>

PHP:

<?php
        if($_POST){
                $email = $_POST['email'];
                $name = $_POST ['name'];
                $company = $_POST ['company'];
                $phone = $_POST ['phone'];
                $location = $_POST ['location'];
                $message = $_POST ['message'];

                // response hash
                $ajaxresponse = array('type'=>'', 'message'=>'');

                try {
                        // do some sort of data validations, very simple example below
                        $all_fields = array('name', 'email', 'message');
                        filter_var($email, FILTER_VALIDATE_EMAIL);

                        foreach($all_fields as $field){
                                if(empty($_POST[$field])){
                                        throw new Exception('Required field "'.ucfirst($field).'" missing input.');
                                }
                        }

                        // ok, if field validations are ok
                        // now Send Email, ect.

                        // let's assume everything is ok, setup successful response
                        $subject = "Someone has contacted you";
                        //get todays date
                        $todayis = date("l, F j, Y, g:i a") ;

                        $message = " $todayis \n
                        Attention: \n\n
                        Please see the message below: \n\n
                        Email Address: $email \n\n
                        Organization: $company \n\n
                        Phone: $phone \n\n
                        Location: $location \n\n
                        Name: $name \n\n
                        Message: $message \n\n

                        ";

                        $from = "From: $email\r\n";


                        //put your email address here
                        mail("...@yahoo.com", $subject, $message, $from);

                        //prep json response
                        $ajaxresponse['type'] = 'success';
                        $ajaxresponse['message'] = 'Thank You! Will be in touch soon';  
                } catch(Exception $e){
                        $ajaxresponse['type'] = 'error';
                        $ajaxresponse['message'] = $e->getMessage();
                }
                // now we are ready to turn this hash into JSON
                print json_encode($ajaxresponse);
                exit;
        }
?>

非常感谢!

最佳答案

您的表单实际上对机器人来说并不安全,因为您没有任何验证码或其他东西。

2 个选项供您选择:

  1. 验证码

验证码 -> 你需要填写一些东西 -> 你可能知道这个!:)

https://www.google.com/recaptcha

  1. 蜜 jar

Honeypot 意味着,您在表单中添加隐藏字段。如果这些隐藏字段发生了变化——您就知道 BOT 已经在您的表单中输入了内容。此外,这比验证码更好,因为您的用户不必填写验证码

我更喜欢 Honeypot,因为我不喜欢表单,当我失败或验证码不可读时,我必须在其中填写一次或什至两次验证码。

http://haacked.com/archive/2007/09/11/honeypot-captcha.aspx/

关于javascript - 检查表单的安全性。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22909104/

相关文章:

javascript - 调用 Binding.getRange() 时出现 InvalidArgument 错误

javascript - Bootstrap 3 模态弹出窗口通过焦点放在顶部防止滚动

php - 在 PHP 中用逗号创建 JS 对象

php - mysql 存储日期和时间的格式是什么?

css - 将CSS应用于Django为textarea生成的表单对象

javascript - lodash 中不区分大小写的 group By

php - 混合 2 个查询来显示独特的优惠券 PHP/MySql

javascript - 使用表单中的提交链接发送值

javascript - React Final Form提交成功后如何将表单设置为 "pristine"

javascript - HTML 模态对话框溢出