我正在尝试在注册页面上放置一个简单的验证码,但我无法验证表单上的验证码部分 如果我将表单上的用户名部分留空并按下注册,它会给我一条消息,请输入用户名等等但是当涉及到验证码部分时,如果我将其留空,它将给出一条消息输入验证码但如果我输入captcha letter但不是它注册的正确的,所以我想尝试结合下面的代码
if ( $real == $guess ) {
die ("enter captcha");
}
和此代码
if ( $real != $guess ) {
die ("Not equal");
}
这是我的注册页码
//Collecting info
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$pass_conf = $_REQUEST['password_conf'];
$email = $_REQUEST['email'];
$date = $_REQUEST['date'];
$guess = $_POST['captcha'];
$real = ( isset( $_SESSION['real'] ) ) ? $_SESSION['real'] : "";
//Here we will check do we have all inputs filled
if ( empty( $username ) ) {
die("Please enter your username!<br>");
}
if ( empty( $password ) ) {
die("Please enter your password!<br>");
}
if ( empty( $pass_conf ) ) {
die("Please confirm your password!<br>");
}
if ( empty( $email ) ) {
die("Please enter your email!");
}
if ( $real == $guess ) {
die ("enter captcha");
}
//Let's check if this username is already in use
$user_check = mysql_query("SELECT username FROM users WHERE username='$username'");
$do_user_check = mysql_num_rows( $user_check );
//Now if email is already in use
$email_check = mysql_query("SELECT email FROM users WHERE email='$email'");
$do_email_check = mysql_num_rows( $email_check );
//Now display errors
if ( $do_user_check > 0 ) {
die("Username is already in use!<br>");
}
if ( $do_email_check > 0 ) {
die("Email is already in use!");
}
//Now let's check does passwords match
if( $password != $pass_conf ) {
die("Passwords don't match!");
}
//If everything is okay let's register this user
$insert = mysql_query("INSERT INTO users (username, password, email) VALUES ('$username', '$password', '$email')");
if ( !$insert ) {
die("There's little problem: ".mysql_error());
}
echo $username.", you are now registered. Thank you!<br><a href=login.php>Login</a> | <a href=index.php>Index</a>";
}
$act = isset($_GET['act']) ? $_GET['act'] : '';
switch( $act ) {
default;
register_form();
break;
case "register";
register();
break;
}
答案 0 :(得分:1)
如下:
if (empty($guess) || $guess !== $real)
{
die("Please enter the correct CAPTCHA.<br>");
}
// the CAPTCHA entered was correct
...
答案 1 :(得分:1)
代码:
if ( $real == $guess ) {
die ("enter captcha");
}
只有当real等于猜测时,表示“输入验证码”。因此,它与你真正想要的相反,从而导致问题的原因。
您需要将其更改为:
if ( $real != $guess ) {
die ("enter captcha");
}
如果真实不等于猜测,则会打印“输入验证码”短语。