if / else语句不能正常工作

时间:2017-07-17 17:51:11

标签: php mysqli

因此,这个if / else语句适用于附带Google Recaptcha的简单登录表单。我已经让Recaptcha部分工作正常,只是当我输入我的用户名和密码时,即使是正确的,我似乎无法登录。这只发生在我添加ReCaptcha之后。 ReCaptcha唯一改变的是if语句检查的另一个条件,不应该导致问题。

这是我的validate.php文件供参考,if语句位于底部:

<?php

if (isset($_POST['submit'])) {
    $userid = $_POST["userid"];
    $password = $_POST["password"];
    $secretkey = "_SECRET_KEY_";
    $responsekey = $_POST["g-recaptcha-response"];
    $useripaddress = $_SERVER["REMOTE_ADDR"];

    $url = "https://www.google.com/recaptcha/api/siteverify?secret={$secretkey}&response={$responsekey}&remoteip={$useripaddress}";
    $response = file_get_contents($url);
    // $response = json_decode($response);
    echo $response;
}

require_once("scripts/thecrab.php"); // This connects to the db

$userid = htmlspecialchars($_POST['userid']);
$password = htmlspecialchars($_POST['password']);

$query = "SELECT userid from users where userid = ? and password = PASSWORD(?)";
$stmt = $pdo->prepare($query);
$stmt->execute([$userid, $password]);

if ($stmt->rowCount() && $response->success === "true") {
    $_SESSION['valid_recipe_user'] = $userid;
    echo "<h2>Log In Successful</h2><br>\n";
    echo "<a href=\"index.php\"><img src=\"images/image-11.png\"></a>\n";
} else {
    echo "<h2>Sorry, your user account was not validated.</h2><br>\n";
    echo "<a href=\"index.php?content=login\">Try again</a><br>\n";
    echo "<a href=\"index.php\">Return to Home</a>\n";
}

这里是确切的if语句和条件:

if ($stmt->rowCount() && $response->success === "true") {
    // Successful Login. Meaning the userid and password are in the database AND the Google ReCAPTCHA response->success has the value of EXACTLY true.
} else {
    // Incorrect Login
}

即使数据库中存在正确的用户名和密码,它也不会执行if语句并跳转到else,而不会登录。

2 个答案:

答案 0 :(得分:3)

Boolean!= String

更改

$response->success === "true"

$response->success === true

Triple equal也检查数据类型。因此布尔true将不等于字符串'true'。顺便说一下,你不需要在这里打字。简单的==会做!

或者坦率地说,这就够了:

if ($stmt->rowCount() && $response->success)

答案 1 :(得分:1)

在您的比较中,您有import QtQuick 2.8 import QtQuick.Window 2.2 import QtQuick.Dialogs 1.2 import Qt.labs.folderlistmodel 2.1 Window { title: qsTr("Test dialog") visible: true width: 640 height: 480 FolderListModel { id: fileModel nameFilters: ["*.*"] onFolderChanged: { console.info(fileModel.get(0, "fileName")) } } FileDialog { id: dialog title: "Select a folder containing image file(s) to classify" selectFolder: true onAccepted: { dialog.close() fileModel.folder = folder } } Component.onCompleted: dialog.open() } 。这不是按值进行比较,而是按类型进行比较。

如果成功$response->success === "true",您可以使用bool。但是,更简单且足够$response->success === true,它会将$response->success == true / string(无论如何)从int自动转换为$response->success