Unity+PHP+MySQ實現使用者登入註冊
轉至(https://blog.csdn.net/MIYIshi/article/details/68950547)
PHP指令碼如下:
<?php
//得到提交的使用者名稱和密碼
$uname = $_POST['uname'];//改成你自己資料庫的欄位
$upass = $_POST['upass'];//改成你自己資料庫的欄位
//action是用來判斷是註冊還查詢
$action = $_POST['action'];
//你自己的資料庫地址
$conn = mysql_connect("127.0.0.1","root","1234");
if(!$conn)
{
die("error");
}
//改成你自己的資料庫名稱
$result = mysql_select_db("lijia",$conn);
if(!$result)
{
mysql_close($conn);
die("error");
}
//判斷使用者是登入還是註冊
if($action == "login")
{
//匹配使用者
$sql = "select *from t_user where uname='".$uname."' and upass='".$upass."'";
//執行
$rs=mysql_query($sql);
if(!$rs)
{
mysql_close($conn);
die("error");
}
$recordCount=mysql_num_rows($rs);
if($recordCount>0)
{
echo "success";
}
else
{
echo "error";
}
}
else if($action == "regist")
{
//設定字符集
$sql = "set names utf8";
//執行
mysql_query($sql);
//檢視使用者是否存在
$sql = "select *from t_user where uname='".$uname."'";
//執行
$rs=mysql_query($sql);
if(!$rs)
{
mysql_close($conn);
die("error");
}
//獲得資料庫行數
$recordCount = mysql_num_rows($rs);
if($recordCount>0)
{
mysql_close($conn);
die("exist");
}else
{
//如果不存在就寫入資料庫
$sql = "insert into t_user(uname,upass) values('".$uname."','".$upass."')";
//執行
$rs=mysql_query($sql);
if(!$rs)
{
mysql_close($conn);
die("error");
}
else
{
echo "success";
}
}
//如果存在就提示
}
else
{
echo "error!";
}
//關閉資料庫
mysql_close($conn);
?>
Unity介面如下:
C#指令碼如下:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
/// <summary>
/// 模擬使用者登入
/// unity + PHP + MySQL
/// </summary>
public class Demo : MonoBehaviour
{
public InputField Name;
public InputField Pass;
public Text ShowText;
private string Url = "http://127.0.0.1/Unity/loginregist.php";
bool action;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
/// <summary>
/// 登入
/// </summary>
public void Btn_Login()
{
action = true;
CreatWFrom (Name.text, Pass.text,action);
}
/// <summary>
/// 註冊
/// </summary>
public void Btn_Regist()
{
action = false;
CreatWFrom (Name.text, Pass.text,action);
}
/// <summary>
/// 建立表單
/// </summary>
/// <param name="name">Name.</param>
/// <param name="pass">Pass.</param>
public void CreatWFrom(string name,string pass,bool action)
{
WWWForm form = new WWWForm ();
form.AddField ("uname",name);
form.AddField ("upass",pass);
if (action == true) {
form.AddField ("action", "login");
} else {
form.AddField ("action", "regist");
}
StartCoroutine (SendPost(Url,form));
}
/// <summary>
/// 提交表單
/// </summary>
/// <returns>The post.</returns>
/// <param name="url">URL.</param>
/// <param name="wForm">W form.</param>
IEnumerator SendPost(string url,WWWForm wForm){
WWW www = new WWW (url,wForm);
yield return www;
if (www.error != null) {
Debug.Log (www.error);
} else {
Debug.Log (www.text);
if (action == false) {
if (www.text == "success") {
ShowText.text = "註冊成功";
} else if (www.text == "exist") {
ShowText.text = "使用者名稱存在";
}
} else {
if (www.text == "success") {
ShowText.text = "登入成功";
} else if (www.text == "error") {
ShowText.text = "登入失敗";
}
}
Invoke ("ShowTextNull",1);
}
}
void ShowTextNull(){
ShowText.text = "";
}
}
完美!
---------------------
作者:程式猿佳佳
來源:CSDN
原文:https://blog.csdn.net/MIYIshi/article/details/68950547
版權宣告:本文為博主原創文章,轉載請附上博文連結!