我无法将我在 Php 中的注册表与验证码集成在一起。我已经尝试将它们与我非常有限的编码知识集成在一起,但是当输入错误的验证码时,它会显示“不正确的验证码”,但不幸的是,它也会在我的 Mysql Db 上输入用户,而无需先验证验证码。
所有相关代码如下图,请大家帮忙,谢谢!
1.Registration.php
<div class="form-title">Sign Up</div>
<div class="form-sub-title">It's free and anyone can join</div>
<form method="post" action="check.php" enctype="multipart/form-data">
<table width="864" align="center" cellpadding = "15">
<tr>
<td>FirstName:</td>
<td><input type="text" name="FirstName" maxlength="10" required="" ></td>
.....................................................................................................................................................................................................
<td> </td>
</td>
</tr> </td>
<td>
<?php
session_start();
echo '<form action="check.php" method="post">';
$rand_int1 = substr(mt_rand(),0,2);
$rand_int2 = substr(mt_rand(),0,1);
$rand_int3 = substr(mt_rand(),0,1);
$captcha_answer = $rand_int1 + $rand_int2 - $rand_int3;
$_SESSION['captcha_answer'] = $captcha_answer;
echo 'What is '.$rand_int1.' + '.$rand_int2.' - '.$rand_int3.'?<br>
<input type="text" name="captcha">
<td><input type="submit" value="Submit" name="registration" class="greenButton"/><img id="loading" src="img/ajax-loader.gif" alt="working.." /></td>
</form>';
?>
</td>
</tr>
2. Check.php
<?php
session_start();
$captcha = $_POST['captcha'];
$captcha_answer = $_SESSION['captcha_answer'];
if($captcha != $captcha_answer) {
echo 'Captcha is incorrect!';
}
else {
echo 'Captcha is correct, congratulations! :)';
}
?>
<?php
if(isset($_POST['registration']))
{
require "connection.php";
$FirstName = strip_tags($_POST['FirstName']);
$LastName = strip_tags($_POST['LastName']);
$Msisdn = $_POST['Msisdn'];
$month = $_POST['month'];
$day = $_POST['day'];
$year = $_POST['year'];
$date = $year . "-" . $month . "-" . $day;
$dob = date('y-m-d', strtotime($date));
$Gender = $_POST['Gender'];
$Faith = $_POST['Faith'];
$City = $_POST['City'];
$MarritalStatus = $_POST['MarritalStatus'];
$Profession =$_POST['Profession'];
$Country = $_POST['Country'];
$query="insert into users set FirstName='".$FirstName."',LastName='".$LastName
."',Msisdn='".$Msisdn."',dob='".$dob."',Gender='".$Gender."',Faith='".$Faith."',City='".$City."',MarritalStatus='".$MarritalStatus."',Profession='".$Profession."',Country='".$Country."'";
mysql_query($query)or die("".mysql_error());
echo "Successful Registration!";
}
?>