2015-06-29 72 views
2

我目前正在研究一個登錄系統(這很好,通過使用兩個文本字段,然後用戶重定向)。但是,由於我正在開發移動應用程序,因此在JSON中執行此操作會更容易,而且我不完全確定從何處開始。我基本上想要做的是使用https請求(從我的應用程序),所以它會請求:https://www.example.com/login.php?username=username&password=passwordPHP MySQL的JSON登錄系統

我已經創建了一個基本的示例,但它不是我想要做的關於傳遞在通過URL的參數中,因爲我的代碼目前正在查找MySQL數據庫中的用戶並以JSON輸出用戶。我想要做的就是通過URL傳遞用戶名&的密碼參數,然後在用戶名/密碼在數據庫中時輸出「成功」或「錯誤」JSON響應。

<?php 
$host  = "localhost"; 
$username = "root"; 
$password = "password"; 
$db_name = "test"; 
$con = mysql_connect("$host", "$username", "$password") or die("cannot connect"); 
mysql_select_db("$db_name") or die("cannot select DB"); 
$sql = "select * from test_table"; 
$result = mysql_query($sql); 
$json = array(); 
if (mysql_num_rows($result)) 
    { 
     while ($row = mysql_fetch_row($result)) 
     { 
    $json['items'] = $row; 
     } 
    } 
mysql_close($db_name); 
echo json_encode($json, JSON_PRETTY_PRINT); 
?> 

我現在已經開始在另一個系統,這是相當多的東西,我希望做的工作的編輯(之後我發現這裏一個例子:http://www.dreamincode.net/forums/topic/235556-how-to-create-a-php-login-with-data-from-mysql-database/),只是每次我要求什麼時間像https://www.example.com/login.php?username=root&password=password - 我得到的JSON錯誤代碼1

<?php 
$host = "localhost"; // Host name 
$db_username = "root"; // Mysql username 
$db_password = "password"; // Mysql password 
$db_name = "test"; // Database name 
$tbl_name = "test_table"; // Table name 

// Connect to server 

mysql_connect("$host", "$db_username", "$db_password") or die("cannot connect"); 

// Select the database 

mysql_select_db("$db_name") or die("cannot select DB"); 

// Get the login credentials from user 

$username = $_POST['username']; 
$userpassword = $_POST['password']; 

// Secure the credentials 

$username = mysql_real_escape_string($_POST['username']); 
$userpassword = mysql_real_escape_string($_POST['password']); 

// Check the users input against the DB. 

$query = "SELECT * FROM test_table WHERE user = '$username' AND password = '$userpassword'"; 
$result = mysql_query($query) or die("Unable to verify user because " . mysql_error()); 
$row = mysql_fetch_assoc($result); 

if ($row['total'] == 1) { 

    // success 

    $response["success"] = 1; 

    // echoing JSON response 

    echo json_encode($response); 
} 
else { 

    // username and password not found 
    // failed 

    $response["failed"] = 1; 

    // echoing JSON response 

    echo json_encode($response); 
} 

?> 
+3

注意一個HTTP POST,一個通常使用表單參數而不是查詢字符串參數。 – J0e3gan

+0

@maytham我現在編輯了我的原代碼以改進功能和安全性,但目前出現錯誤 –

+0

@maytham格式如下:http://codeshare.io/ETRcX&這裏是上述生成JSON響應的代碼:http:/ /codeshare.io/SERMM - 我實際上想要做的是以某種方式安全地將用戶名和密碼(作爲參數)傳遞到URL中>進行MySQL檢查以查看用戶名/密碼是否在數據庫中>如果它在數據庫中那麼應該回顯一個JSON「成功」字符串,如果它不在數據庫中,那麼JSON「失敗」字符串應該回顯 –

回答

2

嘗試改變以下行,它應該工作。按照您描述的方式,我可以開始工作。

1-在這裏,您將獲得URL參數並進行消毒。

$username = $_GET['username']; 
$userpassword = $_GET['password']; 

$username = mysql_real_escape_string($username); 
$userpassword = mysql_real_escape_string($userpassword); 

2 - 在這裏,你算你有多少行有

變化$row = mysql_fetch_assoc($result);

$total_rows = mysql_num_rows($result);

3-在這裏,你檢查,如果你有至少1列。

,並更改if ($row['total'] == 1) {if ($total_rows == 1) {

這應該給輸出{"success":1}

注1:這是解決你的要求和問題,但並不一定意味着在一般正確的方法或完美的解決方案。

注2:我建議你認爲密碼哈希,在獲得,使用庫MySQLi或PDO的STED MySQL和輸入敏感的,而不是使用URL傳遞用戶名和密碼代替POST方法。我建議你看看這個link它描述了我在筆記1中提到的一些事情。

http://www.wikihow.com/Create-a-Secure-Login-Script-in-PHP-and-MySQL

+0

仍導致{「failed」:1}對我來說,即使在做出你所建議的改變之後。也許這與行數有關? –

+0

好的,謝謝,但放入幾個回聲(檢查查詢,結果和行)後,查詢是正確的,並獲取傳遞給它的用戶名和密碼(感謝您提出的代碼更改);結果找到數據庫中的用戶詳細信息,並且只有一行返回,這導致我相信該行正在計數的方式出現問題 –

+0

這意味着,我認爲,我發現問題是爲什麼它總是以失敗告終。那麼,我將如何去計算查詢生成的行數呢? –