我目前正在研究一个登录系统(这很好,通过使用两个文本字段,然后用户重定向)。但是,由于我正在开发移动应用程序,因此在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“失败”字符串应该回显 –