I try to implement the login system on my unity3d game i could make it work: But i have to register the password as the same of username to make it work , i have try to google about it but i could not find any situation close to mine. How can i solve this problem , i want to bee able to register the password different from username. This is my php code
<?php
include_once("db.php");
if (isset($_POST["username"]) && !empty($_POST["username"]) &&
isset($_POST["password"]) && !empty($_POST["password"])){
Login($_POST["username"], $_POST["password"]);
}
function Login($username, $password){
GLOBAL $con;
$sql = "SELECT id,username FROM users WHERE username=? AND password=?";
$st=$con->prepare($sql);
$st->execute(array($username, sha1($password)));//encrypt password
$all=$st->fetchAll();
if (count($all) == 1){
echo "SERVER: ID#".$all[0]["id"]." - ".$all[0]["username"];
exit();
}
//if username or password are empty strings
echo "SERVER: error, invalid username or password";
exit();
}
//if username or password is null (not set)
echo "SERVER: error, enter a valid username & password";
//exit(): means end server connection (don't execute the rest)
?>
This is my code to register
using UnityEngine;
using System.Collections;
public class DataInserter : MonoBehaviour {
public string inputUserName;
public string inputPassword;
public string inputEmail;
string CreateUserURL = "localhost/InsertUser.php";
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Space)) CreateUser(inputUserName, inputPassword, inputEmail);
}
public void CreateUser(string username, string password, string email){
WWWForm form = new WWWForm();
form.AddField("usernamePost", username);
form.AddField("passwordPost", password);
form.AddField("emailPost", email);
WWW www = new WWW(CreateUserURL, form);
}
}
<?php
//Variables for the connection
$servername = "localhost";
$server_username = "root";
$server_password = "mysql";
$dbName = "wizard2019";
//Variable from the user
$username = $_POST["usernamePost"];
$email = $_POST["emailPost"];
$password = sha1($_POST["passwordPost"]);
//$username = "helder";
//$email = "test email";
//$password = "123456";
//Make Connection
$conn = new mysqli($servername, $server_username, $server_password, $dbName);
//Check Connection
if(!$conn){
die("Connection Failed. ". mysqli_connect_error());
}
$sql = "INSERT INTO users (username, email, password)
VALUES ('".$username."','".$email."','".$password."')";
$result = mysqli_query($conn ,$sql);
if(!$result) echo "there was an error";
else echo "Everything ok.";
?>