0

我正在尝试在注册页面上放置一个简单的验证码,但是如果我将表单上的用户名部分留空并按注册,我将无法验证表单上的验证码部分,它会给我一条消息,请输入用户名等等但是当涉及到验证码部分时,如果我将其留空,它将给出一条消息,输入验证码,但如果我输入验证码字母但不是它注册的正确验证码,所以我想尝试结合下面的代码

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;
}
4

2 回答 2

1

怎么样的东西:

if (empty($guess) || $guess !== $real)
{
    die("Please enter the correct CAPTCHA.<br>");
}

// the CAPTCHA entered was correct
...
于 2012-12-27T09:29:44.440 回答
1

编码:

    if ( $real == $guess ) {
   die ("enter captcha");
} 

只有当真实等于猜测时才说“输入验证码”。因此,它与您真正想要的相反,因此也是问题的原因。

您需要将其更改为:

    if ( $real != $guess ) {
   die ("enter captcha");
} 

如果真实等于猜测,它将打印“输入验证码”短语。

于 2012-12-27T09:58:53.127 回答