Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何为iOS应用程序设置数据库?_Php_Ios_Mysql - Fatal编程技术网

Php 如何为iOS应用程序设置数据库?

Php 如何为iOS应用程序设置数据库?,php,ios,mysql,Php,Ios,Mysql,我目前正在制作一个iOS应用程序,用户必须登录或创建一个帐户(然后登录)。为此,我需要以某种方式连接到数据库。数据库类型为SQL,并在XAMPP中使用phpmyadmin创建。目前,它只包含一个名为“user”的表,其属性为“id”、“email”和“password”。我已经创建了两个名为signin.PHP和signup.PHP的PHP脚本,并将其放在c:\xampp\htdoc目录中,以便通过localhost加载脚本。以下是注册脚本: <?php $email = $_GET['

我目前正在制作一个iOS应用程序,用户必须登录或创建一个帐户(然后登录)。为此,我需要以某种方式连接到数据库。数据库类型为SQL,并在XAMPP中使用phpmyadmin创建。目前,它只包含一个名为“user”的表,其属性为“id”、“email”和“password”。我已经创建了两个名为signin.PHP和signup.PHP的PHP脚本,并将其放在c:\xampp\htdoc目录中,以便通过localhost加载脚本。以下是注册脚本:

<?php

$email = $_GET['email'];
$password1 = $_GET['password1'];

$con=mysqli_connect("localhost","root","<mypassword>","app_db");

if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql = "INSERT INTO user (email, password) VALUES ('$email', '$password')";

if ($con->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

mysqli_close($con);

?>
首先:

您“必须”使用https而不是http,否则您的客户密码将在没有任何保护的情况下通过互联网传输

此外: 目前,您的用户可以在您的服务器上执行SQL注入。使用此代码:

if($stmt = $mysqli->prepare("INSERT INTO user (email, password) VALUES (?, ?)"))
{
    $stmt->bind_param('ss',$email,$password); // the variables are of type string->s and they are ordered in the way the questionmarks are in the query!
    $stmt->execute();
    $affected_rows = $stmt->affected_rows;
    $stmt->close();

    if($affected_rows == 1)
    {
        echo "New record created successfully";
    }
}

另外,您可能不应该在“普通测试”中回显响应,而是返回一些机器可读的内容,如json,这样您的应用程序就可以向用户显示有用的结果。

谢谢您的提示和代码。我仍然有点不确定在php中使用这种方法是否是最好的方法。我真的想从一开始就用“正确”的方式来做。没有一种“正确的方式”最适合于每个iOS和android应用程序。尤其重要的是,您公司的开发人员知道什么。如果你有php的人,这是正确的方法。需要注意的一点是:您通常甚至不会将用户输入的真实密码发送到您的文本字段中,而是直接对其进行散列,并且只传输散列,因为没有人会看到(即使是您)实际的用户密码是什么。这样-如果你被黑客攻击-你将永远不会泄漏任何用户密码!是的,我想是的。一些朋友告诉我使用md 5算法来散列密码。因为您使用的是php,所以应该使用本机散列api: