Thursday, January 21, 2016

Game ForgotPassword script Unity c sharp php


ForgotPassword Script :-
========================

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Text.RegularExpressions;
public class ForgotPassword : MonoBehaviour {
    public string username;
    public string emailid;
    string ForgotpasswordForUsername_url = "http://www.yourdomain.com/dr/ForgotPasswordForUsername.php";
    string ForgotpasswordForEmailid_url = "http://www.yourdomain.com/dr/ForgotPasswordForEmailid.php";
    public Text forgotusernametext;
    public Text sendingemailText;
    public string answer;
    public Image ForgotPasswordPanel;
    public Image LoginPanel;
    public InputField usernameInputField;
    public InputField passwordInputField;
    public Button loginButton;
    public Button forgotPasswordButton;
    public Button registerButton;

    IEnumerator RetrievePasswordForUsername()
    {
        username = forgotusernametext.text;
        WWWForm form = new WWWForm ();
        form.AddField ("username1", username);
        WWW download2 = new WWW (ForgotpasswordForUsername_url, form);
        yield return download2;
        answer = download2.text.ToString ();
        Debug.Log ("answer2 is" + answer);
        sendingemailText.text = answer;

        
    }
    IEnumerator RetrievePasswordForEmailid()
    {
        emailid = forgotusernametext.text;
        WWWForm form = new WWWForm ();
        form.AddField ("emailid1", emailid);
        WWW download2 = new WWW (ForgotpasswordForEmailid_url, form);
        yield return download2;
        answer = download2.text.ToString ();
        Debug.Log ("answer2 is" + answer);
        sendingemailText.text = answer;
        
    }
    public void SendButton()
    {
        string UserEmail = forgotusernametext.text;
        if (IsvalidEmailId (UserEmail)) {

            if ((forgotusernametext.text != "")  ) {
                StartCoroutine (RetrievePasswordForEmailid ());
                sendingemailText.text = "Loading.......";
            } 
            else 
            {
                sendingemailText.text = "* invalid  email";
            }
        } 
        else 
        {
            if ((forgotusernametext.text != "")  ) {
                StartCoroutine (RetrievePasswordForUsername ());
                sendingemailText.text = "Loading.......";
            } 
            else 
            {
                sendingemailText.text = "* invalid  Username";
            }

        }


    }
    public void BackButton()
    {
        loginButton.interactable = true;
        forgotPasswordButton.interactable = true;
        registerButton.interactable = true;
        usernameInputField.gameObject.SetActive(true);
        passwordInputField.gameObject.SetActive (true);
        ForgotPasswordPanel.gameObject.SetActive (false);
        LoginPanel.gameObject.SetActive (true);
    }
    public static bool IsvalidEmailId(string InputEmail)
    {
        Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,5})+)$");
        Match match = regex.Match (InputEmail);
        if (match.Success)
            return true;
        else
            return false;
    }
    public static bool IsValidUsername(string InputUsername)
    {
        Regex regex1 = new Regex("^[a-zA-Z]{8,12}$");
        Match match1 = regex1.Match (InputUsername);
        if (match1.Success)
            return true;
        else
            return false;
    }

}

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


<?php session_start();
$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'];
$sql = "SELECT * FROM userinfo where emailid = '$Emailid2' ";
$result  = $conn->query($sql); 
$count=mysqli_num_rows($result);
if($count == 1)
{   
    $rows=mysqli_fetch_array($result);

    $pass = $rows["password"];
    $to = $rows["emailid"];
    $from = "The Red & Black";
    $url = "http://www.thered.black/";
    $body = "Password recovery by The Team Of Red & Black <br>
        your password is : $pass, <br>
    sincerly, <br>
    The Red & black";
    $headers1 = "From: $from\n";
    $headers1 .= "Content-type: text/html;charset=iso-8859-1\r\n";
    $headers1 .= "X-Priority: 1\r\n";
    $headers1 .= "X-MSMail-Priority: High\r\n";
    $headers1 .= "X-Mailer: Just My Server\r\n";

    $subject = "Darkness Password recovered";
    $sentmail = mail($to,$subject,$body,$headers1);
}
else
{

    if (($_POST['emailid1'] != "emailid") ) {
        echo "Your emailid is  not found  to our database";
        # code...
    }

}
if (($sentmail == 1)) {
    echo "Password Sent by Your  email address $to ";
    # code...
}
else
{
    if($_POST['email']!="")
        echo "problem with sending mail";
}

$conn->close(); 

?>





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(); 

?>