我目前正在研究一個登錄系統(這很好,通過使用兩個文本字段,然後用戶重定向)。但是,由於我正在開發移動應用程序,因此在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);
}
?>
注意一個HTTP POST,一個通常使用表單參數而不是查詢字符串參數。 – J0e3gan
@maytham我現在編輯了我的原代碼以改進功能和安全性,但目前出現錯誤 –
@maytham格式如下:http://codeshare.io/ETRcX&這裏是上述生成JSON響應的代碼:http:/ /codeshare.io/SERMM - 我實際上想要做的是以某種方式安全地將用戶名和密碼(作爲參數)傳遞到URL中>進行MySQL檢查以查看用戶名/密碼是否在數據庫中>如果它在數據庫中那麼應該回顯一個JSON「成功」字符串,如果它不在數據庫中,那麼JSON「失敗」字符串應該回顯 –