1 回答
TA贡献1835条经验 获得超7个赞
正如您在评论中指出的,您使用的是 PHP 版本 7.2.19,并且使用的代码有点旧。该代码使用 mysql_ 函数,这些函数在 PHP 5.5.0 版中已弃用,并在 PHP 7 中删除。
我试图更改该代码,它可以在我的电脑(PHP 7.3,MySQL)上运行,因此它也可以在您的服务器上运行。
请使用此代码重新创建您的表(如果可能),因为出于某种原因,您发送的教程不使用自动增量,而且插入数据的位置非常小(varchar(20)作为密码?)
我还添加了 mysqli_real_escape_string 以防止 SQL 注入。我已将 sha256 添加到您的密码中,因为您不想将它们保存为纯文本。
SQL表创建:
CREATE TABLE `users` (
id bigint auto_increment primary key,
email varchar(256) not null,
pwd varchar(256) not null,
constraint users_so_email_uindex
unique (email)
);
和 PHP 代码
<?php
//These variable values need to be changed by you before deploying
//Variables for connecting to your database.
//These variable values come from your hosting account.
$hostname = "your_server";
$username = "username";
$password = "password";
$dbname = "database_name";
//Connect to your database
$db = mysqli_connect($hostname, $username, $password, $dbname) OR DIE ("Unable to connect to database! Please try again later.");
function login($db){
// Escape string and prevent possible SQL injection
$email = $_POST['email'];
$pwd = $_POST['pwd'];
// Prepare SQL statement
$statement = $db->prepare("SELECT * FROM `users` WHERE `email`=?);
$statement->bind_param("s", $email);
$statement->execute();
$row = mysqli_fetch_assoc($result);
if($result->num_rows > 0){
$hashed_pwd = $row['pwd'];
}
if(password_verify($pwd, $hashed_pwd)){
echo 'User verified successfully!';
}
// Close SQL Statement
$statement->close();
echo '{"success":false,"msg":"Email and/or Password Invalid"}';
}
function register($db){
// Escape string and prevent possible SQL injection
$email = $_POST['email'];
$pwd = password_hash($_POST['pwd'], PASSWORD_DEFAULT);
// Prepare SQL statement for check existing user
$statement = $db->prepare("SELECT * FROM `users` WHERE `email`=?");
$statement->bind_param("s", $email);
$statement->execute();
// Get result
$results = $statement->get_result();
// User already exists
if($results && $results->num_rows > 0) {
echo '{"success":false,"msg":"Users Exists"}';
$statement->close();
return;
}
// Close statement for check existing user
$statement->close();
// Prepare SQL statement for insertion
$statement = $db->prepare("INSERT INTO `users` (email, pwd) VALUES (?, ?)");
$statement->bind_param("ss", $email, $pwd);
// Execute insert statement
if($statement->execute()) {
echo '{"success":true,"msg":"User registered successfully"}';
$statement->close();
return;
}
// Something went wrong and user was not registered
echo '{"success":false,"msg":"Unable to register user"}';
}
function forgot($db){
echo '{"success":false,"msg":"Feature not yet implemented"}';
}
$action = null;
if (isset($_GET['action'])) {
$action = $_GET['action'];
}
if ($action == "login") {
login($db);
} elseif ($action == "register") {
register($db);
} elseif ($action == "forgot") {
forgot($db);
}
$db->close();
exit();
**编辑:** 正如评论中所建议的,我已经编辑了代码以使用 SQL 语句 - 谢谢不道德
var url = authphpurl + "?action=login";
List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
formData.Add(new MultipartFormDataSection("email=" + emailText.text + "&pwd=" + passwordText.text));
UnityWebRequest www = UnityWebRequest.Post(url, formData);
yield return www.SendWebRequest();
相同的片段可用于 REGISTER
编辑: PHP 工作,现在统一。我曾经没有统一工作过,但是当我正在寻找解决方案(表单数据不发送到 PHP)时,我看到了这个解决方案。它可能会有所帮助。来源:https : //docs.unity3d.com/Manual//UnityWebRequest-SendingForm.html
- 1 回答
- 0 关注
- 319 浏览
添加回答
举报