PHP停止表单在提交时再次提交

时间:2016-04-21 10:13:09

标签: php forms

我目前正在构建一个到目前为止工作得很好的着陆页...目前我只有2个问题,就是试图阻止表单提交过来,而表单正在提交当前数据和提交表单所需的时间。

表格有两个主要功能,

  1. 将输入的数据存储到MySQL数据库中。 (工作正常。)

  2. 发送电子邮件至我们的电子邮件地址。 (工作正常。)

  3. 我发现在以前的情况下,一些数据库条目和电子邮件使用相同的数据经过大约6或7次,经过试用后,我发现脚本的加载时间会导致用户单击几个提交按钮在他们看到结果之前更多次。我希望表单只能提交一次,无论页面加载时间多长。

    表单PHP验证完全没有时间,几乎是即时的,但是一旦表单完成,实际上需要大约5秒左右的时间,“谢谢,我们会联系。”并存储数据。

    通过阅读代码,您应该很快意识到我已经将关键变量等分配给了“默认”这个词。这是因为我只是小心,脚本本身实际上并没有使用这个占位符。 (显然!)因为我们收集的数据很有价值。

    <?php
    
    session_start();
    //conection: 
    $con=mysqli_connect("localhost","default","default","default");
    // Check connection
    if (mysqli_connect_errno()) {
        echo "Could not connect.";
    }
    // Options
    $yourEmail = "default"; // the email address you wish to receive these mails through
    $yourWebsite = "default"; // the name of your website
    $thanksPage = ''; // URL to 'thanks for sending mail' page; leave empty to keep message on the same page 
    $maxPoints = 4; // max points a person can hit before it refuses to submit - recommend 4
    $requiredFields = "name,telephone,email,postcode"; // names of the fields you'd like to be required as a minimum, separate each field with a comma
    
    // DATABASE STORAGE 
    
    $default = $_POST['default'];
    $default = $_POST['default'];
    $default = $_POST['email'];
    $default = $_POST['default'];
    
    // DO NOT EDIT BELOW HERE
    $error_msg = array();
    $result = null;
    
    $requiredFields = explode(",", $requiredFields);
    
    function clean($data) {
        $data = trim(stripslashes(strip_tags($data)));
        return $data;
    }
    function isBot() {
        $bots = array("test", "test@test.com", "Indy", "Blaiz", "Java", "libwww-perl", "Python", "OutfoxBot", "User-Agent", "PycURL", "AlphaServer", "T8Abot", "Syntryx", "WinHttp", "WebBandit", "nicebot", "Teoma", "alexa", "froogle", "inktomi", "looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory", "Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot", "crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz");
    
        foreach ($bots as $bot)
            if (stripos($_SERVER['HTTP_USER_AGENT'], $bot) !== false)
                return true;
    
        if (empty($_SERVER['HTTP_USER_AGENT']) || $_SERVER['HTTP_USER_AGENT'] == " ")
            return true;
    
        return false;
    }
    
    if ($_SERVER['REQUEST_METHOD'] == "POST") {
        if (isBot() !== false)
            $error_msg[] = "No bots please! UA reported as: ".$_SERVER['HTTP_USER_AGENT'];
    
    
        $points = (int)0;
    
        $badwords = array("adult", "beastial", "bestial", "blowjob", "clit", "cum", "cunilingus", "cunillingus", "cunnilingus", "cunt", "ejaculate", "fag", "felatio", "fellatio", "fuck", "fuk", "fuks", "gangbang", "gangbanged", "gangbangs", "hotsex", "hardcode", "jism", "jiz", "orgasim", "orgasims", "orgasm", "orgasms", "phonesex", "phuk", "phuq", "pussies", "pussy", "spunk", "xxx", "viagra", "phentermine", "tramadol", "adipex", "advai", "alprazolam", "ambien", "ambian", "amoxicillin", "antivert", "blackjack", "backgammon", "texas", "holdem", "poker", "carisoprodol", "ciara", "ciprofloxacin", "debt", "dating", "porn", "link=", "voyeur", "content-type", "bcc:", "cc:", "document.cookie", "onclick", "onload", "javascript");
    
        foreach ($badwords as $word)
            if (
                strpos(strtolower($_POST['default']), $word) !== false || 
                strpos(strtolower($_POST['default']), $word) !== false ||
                strpos(strtolower($_POST['default']), $word) !== false || 
                strpos(strtolower($_POST['default']), $word) !== false
            )
                $points += 2;
    
        if (isset($_POST['nojs']))
            $points += 1;
        if (preg_match("/(<.*>)/i", $_POST['default']))
            $points += 2;   
        if (strlen($_POST['default']) < 3)
            $points += 1;
        if (strlen($_POST['default']) < 3 || strlen($_POST['default'] > 8))
            $points += 2;
        if (preg_match("/[bcdfghjklmnpqrstvwxyz]{7,}/i", $_POST['default']))
            $points += 1;
        // end score assignments
    
        foreach($requiredFields as $field) {
            trim($_POST[$field]);
    
            if (!isset($_POST[$field]) || empty($_POST[$field]) && array_pop($error_msg) != "Please fill in all the required fields and submit again.\r\n")
                $error_msg[] = "Please fill in all the required fields and submit again.";
        }
    
        if (!empty($_POST['default']) && !preg_match("/^[a-zA-Z-'\s]*$/", stripslashes($_POST['default'])))
            $error_msg[] = "Your default can not contain special characters.\r\n";
        if (!empty($_POST['default']) && !preg_match('/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+' . '(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i', strtolower($_POST['default'])))
            $error_msg[] = "That is not a valid default.\r\n";
    
        if ($error_msg == NULL && $points <= $maxPoints) {
    
    
                $inf_date = date("D dS M y @ H:i:s");
                $ip_ad = $_SERVER['REMOTE_ADDR'];
    
              mysqli_query($con,"INSERT INTO default (id,default,default,default,default,default,default) 
                VALUES ('','default','default','default','default','default','default')");
    
    
            $subject = "default";
    
            $message = "default"."\r\n"."\r\n";
            foreach ($_POST as $key => $val) {
                if (is_array($val)) {
                    foreach ($val as $subval) {
                        $message .= ucwords($key) . ": " . clean($subval) . "\r\n";
                    }
                } else {
                    $message .= ucwords($key) . ": " . clean($val) . "\r\n";
                }
            }
            $message .= "\r\n";
            $message .= 'IP: '.$_SERVER['REMOTE_ADDR']."\r\n";
            $message .= 'Browser: '.$_SERVER['HTTP_USER_AGENT']."\r\n\n";
    
            if (strstr($_SERVER['SERVER_SOFTWARE'], "Win")) {
                $headers   = "From: $yourEmail\r\n";
            } else {
                $headers   = "From: $yourWebsite <$yourEmail>\r\n"; 
            }
            $headers  .= "Reply-To: {$_POST['default']}\r\n";
    
            if (mail($yourEmail,$subject,$message,$headers)) {
    
    
    
                if (!empty($thanksPage)) {
                    header("Location: $thanksPage");
                    exit;
                } else {
                    $result = '<i class=icon-thumbs-up-1></i> Thanks, we\'ll be in touch shortly.';
                    $disable = true;
                }
            } else {
                $error_msg[] = 'Your mail could not be sent this time. ['.$points.']';
            }
        } else {
            if (empty($error_msg))
                $error_msg[] = 'Your mail looks too much like spam, and could not be sent this time. ['.$points.']';
        }
    }
    function get_data($var) {
        if (isset($_POST[$var]))
            echo htmlspecialchars($_POST[$var]);
    }
    ?>
    

0 个答案:

没有答案