简体   繁体   English

检查表格的安全性。

[英]Check the security of form.

My account was suspended because of SPAM several times and my host provider told me to check my website security. 我的帐户因SPAM多次被暂停,我的托管服务提供商要求我检查我的网站安全性。 May be my forms are not secured enough. 可能是我的表格不够安全。 Do you think that this form can be used to send spam? 您是否认为此表格可用于发送垃圾邮件?

Here is my code: 这是我的代码:

<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: 的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:

<?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;
        }
?>

Many thanks! 非常感谢!

  1. I have a simple approach to stopping spammers which is 100% effective, at least in my experience, and avoids the use of reCAPTCHA and similar approaches. 我有一个简单的制止垃圾邮件发送者的方法,至少在我的经验中,这种方法是100%有效的,并且避免使用reCAPTCHA和类似的方法。 I went from close to 100 spams per day on one of my sites' html forms to zero for the last 5 years once I implemented this approach. 一旦实施此方法,过去5年中,我每天从网站的html表单上的近100个垃圾邮件变为零。

  2. another option is what I did is to use a hide field and put the time stamp on it and then compare to the time stamp on the PHP side, if it was faster than 15 seconds (depends on how big or small is your forms) that was a bot... 另一个选择是我做的是使用一个隐藏字段并将时间戳记放到它上面,然后将其与PHP端的时间戳记进行比较(如果它快于15秒(取决于表单的大小)),是个机器人

Your form would actually be not safe against bots, because you dont got any captcha or something. 您的表格实际上对机器人来说并不安全,因为您没有任何验证码或其他东西。

2 Options for you: 2个选项供您选择:

  1. Captcha 验证码

Captcha -> you got something to fill in -> you probably know this!:) 验证码->您需要填写一些内容->您可能知道这一点!

https://www.google.com/recaptcha https://www.google.com/recaptcha

  1. Honeypot 蜜罐

Honeypot means, you are adding hidden fields in your form. 蜜罐意味着您要在表单中添加隐藏字段。 And if those hidden fields have changed - you know that a BOT has entered content in your form. 如果这些隐藏字段已更改-您知道BOT已在表单中输入内容。 Aswell, this is better than Captchas, because your User doesnt has to fill in a Captcha 另外,这比验证码更好,因为您的用户不必填写验证码

I would prefer Honeypot, because I don't like forms, where i have to fill in a Captcha once or even twice, when I failed or the captcha wasnt readable. 我更喜欢Honeypot,因为我不喜欢表格,当我失败或验证码不可读时,我必须填写一次甚至两次验证码。

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

Taking clue from the suggestions above, I am just putting a ready code for you to use. 从以上建议中获得线索,我只是准备好一个可供您使用的代码。

HTML 的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="checkbot" type="hidden" name="timestamp" value="" />

<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>

Javascript Java脚本

<script type="text/javascript">
$(document).ready(function () {
    /*Set current time on the hidden field.*/
    $('#checkbot').val($.now());

    $('#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>

PHP 的PHP

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

                //If Time difference is less than 15 sec it's a bot
                if($time_diff < 15){
                exit;
                }


                // 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;
        }
?>

In theory it can be used to send spam, because there are only checks if fields have values and as long the fields have a value, it does not care whether the input was human or a bot. 从理论上讲,它可以用于发送垃圾邮件,因为仅检查字段是否具有值,并且只要字段具有值,它就不会在乎输入是人类还是机器人。 You could improve the security by adding captcha codes ( http://www.captcha.net/ ), to validate if an individual filling in your form is a human. 您可以通过添加验证码( http://www.captcha.net/ )来提高安全性,以验证填写表格的个人是否是人类。

Try using this Spam Checker . 尝试使用此垃圾邮件检查器 Useful program written in Java which looks up for spam IP Addresses using DNS lookups. 用Java编写的有用程序,它使用DNS查找来查找垃圾邮件IP地址。 Hope so it helps. 希望如此会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM