2017-07-17 126 views
0

所以這個if/else語句是與谷歌的Recaptcha一個簡單的登錄表單連接。我有Recaptcha的一部分工作正常,只是當我輸入我的用戶名和密碼,即使正確,我似乎無法登錄。這隻發生在我添加ReCaptcha後。 ReCaptcha唯一改變的是if語句檢查的另一個條件,不應該引起問題。if/else語句正確

下面是引用我的validate.php文件時,如果有問題的語句是在底部:

<?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語句,並跳轉到其他人,不登錄我進去。

+3

**切勿將明文密碼!**請使用*** PHP的[內置函數(HTTP ://jayblanchard.net/proper_password_hashing_with_PHP.html)***來處理密碼安全。如果您使用的PHP版本低於5.5,則可以使用'password_hash()'[兼容包](https://github.com/ircmaxell/password_compat)。 ***在散列之前,不需要[escape passwords](http://stackoverflow.com/q/36628418/1011527)***或使用其他任何清理機制。這樣做*更改密碼並導致不必要的附加編碼。 –

+2

我希望**祕密鑰匙,不是你真正的鑰匙。 – chris85

+0

'如果($ stmt-> rowCount時()'你怎麼認爲的回報? –

回答

3

布爾!=字符串

變化

$response->success === "true" 

$response->success === true 

三重等於檢查的數據類型爲好。所以布爾型true將不會等於字符串'true'。順便說一句,你不需要在這裏打字檢查。簡單的==就可以做到!


還是坦率地說,這僅僅是不夠的:

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

謝謝!這很有道理,因爲Google只會返回一個布爾值,0或1,true或false,所以==就足夠了,所以不需要檢查類型,因爲除非Google決定這麼做,否則它將永遠不會改變。我完全忽略了一個事實,即我正在檢查包含「true」的字符串。那麼在刪除引用時,我仍然會收到同樣的問題。即使使用正確的用戶名和密碼以及填寫ReCaptcha,我仍然無法登錄。 – Brxxn

1

在你比較,你有$response->success === "true"。這不是按價值比較,而是按類型比較。

如果成功是bool,你可以使用$response->success === true。然而,簡單,夠$response->success == true,這將自動轉換string/int(任何)從$response->successbool