为什么 PHP 中的 NOR 运算符不起作用?
Why does this NOR operator in PHP not working?
我正在尝试在我的网站上创建一个注册页面,并且我已完成所有过滤代码。所以我在下面的代码中试图做的是确保数据库中不存在用户名并从那里继续。问题是这样的:我正在用 $error
和 $captchaError
做一个 NOR 运算符,但是即使当我打印它们时, 它们的值都是 false, if 语句中的代码没有以 运行ning 结束,我真的不明白为什么。这是代码:
<?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 语句 运行,但出于某种原因没有,为什么?
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';
将输出:
ok
no
在某些情况下,它甚至会打印:
Use of undefined constant captchaError - assumed 'captchaError' in [...]
有些人可能认为这是获得 nor
测试的(可以说)更好的方法:
(! ($var1 || $var) )
我正在尝试在我的网站上创建一个注册页面,并且我已完成所有过滤代码。所以我在下面的代码中试图做的是确保数据库中不存在用户名并从那里继续。问题是这样的:我正在用 $error
和 $captchaError
做一个 NOR 运算符,但是即使当我打印它们时, 它们的值都是 false, if 语句中的代码没有以 运行ning 结束,我真的不明白为什么。这是代码:
<?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 语句 运行,但出于某种原因没有,为什么?
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';
将输出:
ok
no
在某些情况下,它甚至会打印:
Use of undefined constant captchaError - assumed 'captchaError' in [...]
有些人可能认为这是获得 nor
测试的(可以说)更好的方法:
(! ($var1 || $var) )