-1

我正在尝试在我的网站上创建一个注册页面,并且我已经完成了所有过滤代码。所以我在下面的代码中试图做的是确保数据库中不存在用户名并从那里继续。问题是这样的:我正在做一个 NOR 运算符,$error但是$captchaError即使当我打印它们时,它们的值都是 false,if 语句中的代码最终没有运行,我真的不明白为什么. 这是代码:

<?php
    $error = false;
    $captchaError = false;
    if($_POST) {
        $username = $_POST["username"];
        if (!preg_match("/^[0-9A-Za-z_]{3,16}+$/", $username)) {
            $error = true;
        }
        $email = $_POST["email"];
        if (!filter_var($email, FILTER_VALIDATE_EMAIL) || strlen($email) > 50) {
            $error = true;
        }
        $password = $_POST["password"];
        if(!preg_match("/^[-0-9A-Za-z!@#$%^&*_+=]{6,}+$/", $password)) {
            $error = true;
        }
        $captcha = $_POST['g-recaptcha-response'];
        $response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$SECRETKEY&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
        if(!$captcha || $response['success'] == false) {
            $captchaError = true;
        }
        if((!$error) && (!captchaError)) {
            include("../res/db.php");
            $prep = $conn->prepare("SELECT * FROM users WHERE name=?");
            $prep->bind_param("s", $username);
            $prep->execute();
            $prep->store_result();
            echo "why doesn't this run??";
            var_dump($prep->num_rows);
        }
    }
?>

这是形式:

<form method="POST" action="">
    <?php if($error) { echo "<p class=\"text-danger text-center\">Error: Something went wrong while submitting your form</p>";}?>
    <div class="form-group">
        <h2 class="text-center">Username </h2>
        <input value="<?php if($captchaError) {echo $username;}?>" class="form-control input-lg" pattern="[0-9A-Za-z_]{3,16}" title="You must use between 3 and 16 legal characters (0-9,a-z,A-Z and _)" type="text" name="username" maxlength="16" required>
    </div>
    <div class="form-group">
        <h2 class="text-center">Email</h2>
        <input value="<?php if($captchaError) {echo $email;}?>" class="form-control input-lg" type="email" name="email" maxlength="50" required>
    </div>
    <div class="form-group">
        <h2 class="text-center">Password</h2>
        <input class="form-control input-lg" type="password" name="password" pattern="[-0-9A-Za-z!@#$%^&*_+=]{6,}" title="You must use more than 6 legal characters (0-9,a-z,A-Z and !@#$%^&*_-+=)" required>
    </div>
    <div class="text-center">
        <?php if($captchaError) { echo "<p class=\"text-danger text-center\">Error: You must fill out the captcha</p>";}?>
        <div class="g-recaptcha" data-sitekey="{MYPUBLICKEY}"></div>
    </div>
    <div class="text-center">
        <button type="submit" class="btn btn-primary btn-lg">Submit</button>
    </div>
</form>

请记住,每当我打印的值$error$captchaError它们都是假的,这应该使 if 语句运行,但由于某种原因它没有,为什么?

4

1 回答 1

1

TL;DR:$captchaError.


问题是您正在使用

if((!$error) && (!captchaError)) {
//               /\

代替

if((!$error) && (!$captchaError)) {
//               /\

看一个测试:

$error = false;
$captchaError = false;

echo ((!$error) && (!$captchaError)) ? 'ok' : 'no';
echo PHP_EOL;
echo ((!$error) && (!captchaError)) ? 'ok' : 'no';

将输出:

好的

在某些情况下,它甚至会打印:

使用未定义的常量 captchaError - 在 [...] 中假定为“captchaError”

运行测试


有些人可能认为这是一种(可以说)更好的nor测试方式:

(! ($var1 || $var) )
于 2016-07-04T21:50:28.603 回答