如何验证验证码字段代码?

时间:2015-01-23 13:11:45

标签: php jquery mysql

我正在尝试验证验证码代码字段,因此如果他们输入的代码是正确的,它会将它们带到带有下载链接的感谢页面,但是如果他们输入的安全代码不正确,那么他们会看到一条抱歉的消息并返回上一页。

我面临的问题是当我将验证码输入此字段并单击提交时,数据始终为否。

我的表格如下:

<form action="" name="downloadform" id="downloadform"  class="downloadform" method="post">
        <div class="field">
        <input name="name" type="text" id="name" class="input name" placeholder="Name..." />
        </div>
        <div class="field">
        <input name="company" type="text" id="company" class="input company" placeholder="Company..." />
        </div>
        <div class="field">
        <input name="tel" type="text" id="tel" class="input tel" placeholder="Telephone..." />
        </div>
        <div class="field">
        <input name="email" type="text" id="email" class="input email" placeholder="Email Address..." />
        </div>
        <div class="field">
        <img src="/CaptchaSecurityImages.php" alt="Captcha" class="captcha" />
        <input type="text" name="sec_code" id="sec_code" class="input sec_code" placeholder="Please enter the characters above" />
        </div>
        <div class="field">
        <div class="medium secondary btn"><input type="submit" name="Submit2" value="Send Request" class="btn" id="downloadbtn" /></div>
        <input type="hidden" name="product" id="product" class="product" value="<?php echo $page[3]; ?>" />
        </div>
    </form>

我的ajax表单文件如下所示:

$(function() {

filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

$("#downloadbtn").click(function() {

    var name = $("#name").val();
    var company = $("#company").val();
    var tel = $("#tel").val();
    var email = $("#email").val();
    var product = $("#product").val();
    var sec_code = $("#sec_code").val();

    if (name == "") {
      $("#name").focus();
      $("#name").val("");
      $("#name").css({background:"#b72a18", color:"#fff"});
      return false;
    }
    if (company == "") {
      $("#company ").focus();
      $("#company ").val("");
      $("#company ").css({background:"#b72a18", color:"#fff"});
      return false;
    }
    if (tel == "") {
      $("#tel").focus();
      $("#tel").val("");
      $("#tel").css({background:"#b72a18", color:"#fff"});
      return false;
    }
    if (!filter.test(email)) {
      $("#email").focus();
      $("#email").val("");
      $("#email").css({background:"#b72a18", color:"#fff"});
      return false;
    }
    if (product == "") {
      $("#product").focus();
      $("#product").val("");
      $("#product").css({background:"#b72a18", color:"#fff"});
      return false;
    }
    if (sec_code == "") {
      $("#sec_code").focus();
      $("#sec_code").val("");
      $("#sec_code").css({background:"#b72a18", color:"#fff"});
      return false;
    }

    $('.downloadform').html('<center><img src="/images/ajax-loader.gif" style="padding:20px;"></center>');

    var dataString = '&name=' + name + '&tel=' + tel + '&company=' + company + '&email=' + email + '&product=' + product + '&sec_code=' + sec_code + '&type=download';
    //alert (dataString);return false;

    $.ajax({
      type: "POST",
      url: "/process_download.php",
      data: dataString,
      datatype: 'json',
      success: function(data) {
        $('.downloadform').html('<center><img src="/images/ajax-loader.gif" style="padding:20px;"></center>')
        .hide()
        .fadeIn(1500, function() {});
        setTimeout(function ()
        {   
            $(window.location).attr('href', '/process.php?download=' + data.product + '&sec_code=' + data.sec_code);
        }, 2000);
  }
 });
return false;
});
});

然后我有我的进程下载文件:

<?php
session_start();
header("Content-type: application/json");
ob_start();
include('inc/connection.php');
$product = $_POST['product'];
$sec_code = $_SESSION['security_code'] == $_POST['sec_code'] ? 'yes' : 'no';

ob_end_clean();
$data = array('product'=>$product,'sec_code'=>$sec_code);
print json_encode($data);
die();
?>

然后是最后的过程:

<?php 
session_start();
include('inc/connection.php');

$sec_code = $_GET['sec_code'];

$proddownlink = $_GET['download'];

$proddownl = str_replace("_", " ", $_GET['download']);

$proddownl = ucwords($proddownl);

if ($sec_code == 'no') {
    $message = '<p>Security code is wrong. Please <a href="/products/' . $proddownlink . '">click here</a> to return back.</p>';
} else {
    $message = '<p>Thank you for downloading ' . $proddownl . ' Data Sheet.</p>
    <p>Please <a href="/pdfs/' . $_GET['download'] . '.pdf">click here</a> to download ' . $proddownl . ' PDF.</p>';

include_once('inc/connection.php');
include_once('inc/class.phpmailer.php');

$name = $_POST['name'];
$company = $_POST['company'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$product = $_POST['product'];
$sec_code = $_POST['sec_code'];
$type = $_POST['type'];

$bodytext = "<ul>
<li><strong>Name:</strong> $name</li>
<li><strong>Company:</strong> $company</li>
<li><strong>Telephone Number:</strong> $tel</li>
<li><strong>Email Address:</strong> $email</li>
<li><strong>Area of Interest:</strong> $product</li>
</ul>";

$subject = "New Enquiry";

$query = "insert into user_email set name = '$name', email = '$email', tel = '$tel', type = '$type', message = '$bodytext'";

$result = $conn->query($query);
if(!$result) die($conn->error);

$mail             = new PHPMailer(); // defaults to using php "mail()"

$body             = $bodytext;

$mail->From       = "sales@fidelitysystems.co.uk";
$mail->FromName   = $name;

$mail->Subject    = $subject;
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";

$mail->MsgHTML($body);

#$mail->AddAddress("sales@fidelitysystems.co.uk");
$mail->AddAddress("craig@arrivaldesign.co.uk");

$mail->IsSMTP();
$mail->SMTPAuth = "true";
$mail->Username = "postmaster@arrivalbusiness.co.uk";
$mail->Password = "edward";
$mail->Host     = "mail.arrivalbusiness.co.uk";
$mail->Port = 587;

if(!$mail->Send()) {
      echo "Mailer Error: " . $mail->ErrorInfo;
}
}
?>

0 个答案:

没有答案