2016-02-29 153 views
1

我有一個問題在這裏,我的形式使用驗證驗證碼,,不能這樣做意味着,如果驗證碼空,形式仍然可以發送到我的數據庫驗證,,形式不能使用驗證碼

這是我的PHP連接發送郵件到我的數據庫

<?php 
$servername = "localhost"; 
$dbusername = "root"; 
$dbpassword = ""; 
$dbname = "test"; 
//Define the database 

session_start(); 
$name = $_POST['name']; 
$email = $_POST['email']; 
$message = $_POST['message']; 
//Pick data from table in HTML 

$conn = new mysqli($servername, $dbusername, $dbpassword, $dbname); 

if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 
if (empty($name)){ 
    echo '<script>alert("Name cannot be blank. Please insert your name."); 
     window.history.go(-1); 
     </script>'; 
    die(); 
} 
if (empty($email)){ 
    echo '<script>alert("Your message has been sent. Thank you for your cooperation."); 
     window.history.go(-1); 
     </script>'; 
    die(); 
} 
if (empty($message)){ 
    echo '<script>alert("Please tell us what is in your mind. Thank you."); 
     window.history.go(-1); 
     </script>'; 
    die(); 
} 
if(empty($_SESSION['code']) || 
    strcasecmp($_SESSION['code'], $_POST['code']) != 0) 
{ 
    //Note: the captcha code is compared case insensitively. 
    //if you want case sensitive match, update the check above to 
    // strcmp() 
    $errors .= "\n The captcha code does not match!"; 
} 
//Error message of the form 

$sql = "INSERT INTO testingdb (Name, Email, Message) 
VALUES ('$name', '$email', '$message')"; 

if($conn->query($sql) === TRUE) { 
    echo '<script> 
      alert("Your message has been sent. Thank you for your cooperation."); 
     </script>'; 
    header("Location: {$_SERVER['HTTP_REFERER']}"); 
     function goback(){ 
      echo '<script>alert("Your message has been sent. Thank you!");</script>'; 
      header("Location: {$_SERVER['HTTP_REFERER']}"); 
      exit; 
     } 
     goback(); 
} else { 
    echo "Error " . $sql . "<br>" . $conn->error; 
} 
//Insert data from HTML table to database 

// Function to validate against any email injection attempts 
function IsInjected($str) 
{ 
    $injections = array('(\n+)', 
       '(\r+)', 
       '(\t+)', 
       '(%0A+)', 
       '(%0D+)', 
       '(%08+)', 
       '(%09+)' 
      ); 
    $inject = join('|', $injections); 
    $inject = "/$inject/i"; 
    if(preg_match($inject,$str)) 
    { 
    return true; 
    } 
    else 
    { 
    return false; 
    } 
} 
?> 

$conn->close(); 
?> 

代碼這是我在HTML

 <form id="contact_form" action="connection.php" method="POST" enctype="multipart/form-data"> 
    <div class="row"> 
     <input id="name" class="input" name="name" type="text" value="" size="30" placeholder="Your Name*" onFocus="this.placeholder = ''" onBlur="this.placeholder = 'Your Name *'" /><br /> 
    </div> 
    <div class="row" style="margin-top:10px"> 
     <input id="email" class="input" name="email" type="text" value="" size="30" placeholder="Your Email*" onFocus="this.placeholder = ''" onBlur="this.placeholder = 'Your Email *'" /><br /> 
    </div> 
    <div class="row" style="margin-top:10px"> 
     <textarea id="message" class="input" name="message" rows="7" cols="31" placeholder="Your Messages *" onFocus="this.placeholder = ''" onBlur="this.placeholder = 'Your Messages *'" ></textarea><br /> 
    </div> 
    <img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' style="margin-top:10px" ><br> 
     <input id="code" class="input" name="code" type="text" value="" size="32" placeholder="Your Code Here *" onFocus="this.placeholder = ''" onBlur="this.placeholder = 'Your Code Here *'" style="margin-top:10px"><br> 
    <p> <button id="submit_button" type="submit" style="cursor:pointer">Submit</button> 
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <button id="reset_button" type="reset" style="cursor:pointer">Reset</button></p> 
</form> 

我在編在這裏漂亮的新形式,所以有人可以幫助我嗎?並且對不起,如果我的英語不好。 我以此鏈接爲例http://webdesignpub.com/html-contact-form-captcha/

+0

你從教程中遺漏了大量代碼,再次閱讀它 – 2016-02-29 02:07:21

回答

1

您需要設置$ _SESSION ['code'您可以將它與$ _POST ['code']進行比較,當您在下一個請求中檢索發佈的數據時。

我不知道captcha_code_file.php做什麼,因爲您還沒有它,但如果它只是顯示的隨機數,然後,如果你改變它可能工作:

<img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ... 

<img src="captcha_code_file.php?rand=<?php echo $_SESSION['code'] = rand(); ?>" id='captchaimg' ... 
+0

'$ _SESSION',而不是'$ SESSION' – andrewsi

+0

確實。非常感謝你。 – OscarJ