doula4096 2014-04-07 09:56
浏览 56
已采纳

检查表单的安全性。

My account was suspended because of SPAM several times and my host provider told me to check my website security. 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:

<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 

                        Attention: 


                        Please see the message below: 


                        Email Address: $email 


                        Organization: $company 


                        Phone: $phone 


                        Location: $location 


                        Name: $name 


                        Message: $message 



                        ";

                        $from = "From: $email
";


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

  • 写回答

5条回答 默认 最新

  • duanlu0386 2014-04-07 10:07
    关注

    Your form would actually be not safe against bots, because you dont got any captcha or something.

    2 Options for you:

    1. Captcha

    Captcha -> you got something to fill in -> you probably know this!:)

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单