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 = "";
	}
}

完美!
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页