繁体   English   中英

PHP MySQL JSON 登录系统

[英]PHP MySQL JSON Login System

我目前正在开发一个登录系统(通过使用两个文本字段可以很好地工作,然后用户被重定向)。 但是,由于我正在开发移动应用程序,因此在 JSON 中执行此操作会容易得多,而且我不完全确定从哪里开始。 我基本上想要做的是使用 https 发布请求(来自我的应用程序)所以它会请求: https://www.example.com/login.php?username=username&password=password

我已经创建了一个基本示例,但这并不是我想要通过 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);
}

?>

尝试更改以下几行,它应该可以工作。 我让它工作,正如你描述的那样。

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:我建议你考虑密码散列,post 方法而不是 get,在 mysql 的 sted 中使用 mysqli 或 PDO 并输入敏感,而不是使用 URL 来传递用户名和密码。 我建议您查看此链接,它描述了我在 note1 中提到的一些内容。

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

您可以创建一个没有 MYSQL 数据库的登录/注册系统,只需使用 .json 文件,系统就可以在没有数据库的情况下运行。它既安全又简单

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM