Thursday, 21 January 2016

Game Login script Unity c sharp php


Login Script :-
================

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class LoginScript : MonoBehaviour {
    public InputField usernameInputField;
    public InputField passwordInputField;
    public string username;
    public string password;
    public string answer;
    public string answer2;
    public string[] splitWords;
    string errorString;
    public Text errormsg;
    public Text DidnotenterText;
    string login_url = "http://www.yourdomain.com/dr/Login.php";
    public Image LoginSuccessfulPanel;
    public Image RegisterPanel;
    public Image LoginPanel;
    public Image RegistrationSuccessfullPanel;
    public Image ForgotPasswordPanel;
    public Text forgotusernametext;
    public Text sendingemailText;

    public Button loginButton;
    public Button forgotPasswordButton;
    public Button registerButton;

    public void Start () {
        LoginSuccessfulPanel.gameObject.SetActive (false);
        RegisterPanel.gameObject.SetActive (false);
        RegistrationSuccessfullPanel.gameObject.SetActive (false);
        ForgotPasswordPanel.gameObject.SetActive (false);

    }
    public void LoginButton()
    {
        if ((usernameInputField.text != "")  && (passwordInputField.text != "")) {
            StartCoroutine (RetrieveData ());
        } else {
            DidnotenterText.text = "* invalid  Username and Password";
        }
    }
    IEnumerator RetrieveData()
    {
        username = usernameInputField.text;
        password = passwordInputField.text;
        
        WWWForm form = new WWWForm ();
        form.AddField ("username1", username);
        form.AddField ("password1", password);

        WWW download1 = new WWW (login_url, form);
        
        yield return download1;
        answer = download1.text.ToString ();
        Debug.Log ("Ans" + answer);


        if (answer != "MisMatch") {
            DidnotenterText.text = " Loading .....";
            DidnotenterText.text = answer.ToString();
            splitWords = answer.Split ('-');

            /*GameControl.control.Coins = int.Parse(splitWords [0]);
            GameControl.control.Health = int.Parse(splitWords [1]);
            GameControl.control.Spirit = int.Parse(splitWords [2]);*/

            //Application.LoadLevel("CharSelection");
        }

        if (answer == "MisMatch") 
        {
            DidnotenterText.text = "* invalid  Username and Password";
            LoginSuccessfulPanel.gameObject.SetActive(false);
        }

        if (!string.IsNullOrEmpty (download1.error)) {
            print ("Error downloading: " + download1.error);
            errorString = download1.error;
            errormsg.text = errorString;

        } else {

        }
    }
    public void OkButton()
    {
        LoginSuccessfulPanel.gameObject.SetActive (false);
        ForgotPasswordPanel.gameObject.SetActive (false);
        usernameInputField.text = "";
        passwordInputField.text = "";

    }
    public void RegisterButton()
    {
        DidnotenterText.text = "";
        usernameInputField.text = "";
        passwordInputField.text = "";
        LoginSuccessfulPanel.gameObject.SetActive (false);
        RegisterPanel.gameObject.SetActive (true);

    }

    public void ForgotPassword()
    {
        loginButton.interactable = false;
        forgotPasswordButton.interactable = false;
        registerButton.interactable = false;
        usernameInputField.gameObject.SetActive(false);
        passwordInputField.gameObject.SetActive (false);
        ForgotPasswordPanel.gameObject.SetActive (true);
    }
        
    
}



Server Side Script :-
======================

<?php 
$servername = "Your_ServerName"; 
$username = "Your_UserName";
$password = "************";
$dbname = "Your_DB_Name";


$conn = new mysqli($servername, $username, $password, $dbname); 

if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 


$Username2=$_POST['username1'];
$Emailid2 = $_POST['emailid1'];
$Password2 = $_POST['password1'];


if ($result->num_rows > 0) 
{ 
    $exits = 1; 
} 
else 
{ 
    $sql1 = "SELECT * FROM userinfo where username = '$Username2' AND password = '$Password2'";
    $result = $conn->query($sql1);
}


if ( $row = $result->fetch_assoc())
{ 
    echo $row["Coins"]."-".$row["Health"]."-".$row["Spirit"];
} 
else
{
    echo "MisMatch";
}
$conn->close(); 

?>






1 comment: