PHP会话正在重新启动而不是恢复

时间:2015-04-04 16:47:30

标签: php session unity3d

我是unity3d为我的游戏制作登录/注销屏幕。我在登录期间为会话变量赋值。它在浏览器中工作正常,但如果我从统一调用注销,它总是说“用户没有登录”。

这意味着会话变量返回NULL或会话正在logout.php内重新启动而不是恢复。我该如何解决这个问题?

的login.php

<?php
session_start();

 $servername="localhost";
 $username="root";
 $password="xxxx";
 $database="database";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // =============================================================================
    $unityHash = $_POST["unity_hash"];
    $phpHash = "hashcode"; // same code in here as in your Unity game

    $player = $_POST["form_name"]; //I use that function to protect against SQL injection
    $password = $_POST["form_password"];

    if(!$player || !$password) {
        echo "Login or password cant be empty.";
    } else {
        if ($unityHash != $phpHash){
            echo "HASH code is diferent from your game, you infidel.";
        } else {
            $sql = $conn->prepare("SELECT password FROM players WHERE player = '$player' LIMIT 1");
            $sql->execute();            
            if($sql->rowCount() > 0)
            {
                    $row = $sql->fetch(PDO::FETCH_ASSOC);
                    if(password_verify($password, $row['password'])){
                        echo "successfully logged in";
                        //update the state
                        $stmt = $conn->prepare("UPDATE players SET state='1' WHERE player= '$player' AND password = '$row[password]'");
                        $stmt->execute();   
//SESSION VARIABLE ASSIGNMENT  
                            $_SESSION["form_name"] = $player;
                            $_SESSION["form_password"] = $row['password'];

                        }
                        else{
                        echo "Wrong password";
                        }

                }
                else{
                    echo "Data invalid - cant find name.";
                }               
            }
        }
        }
    catch(PDOException $e)
        {
        echo "Error: " . $e->getMessage();
        }

    $conn = null;
    ?>

Logout.php

<?php
session_start();
echo $_SESSION["form_password"];

if(isset($_SESSION["form_name"])){
    unset($_SESSION["form_name"]);
    unset($_SESSION["form_password"]);
    echo "successfully logged out"; 
}

else{
echo "user not logged in"; 
}        
?>

如果我尝试打印$ _SESSION [“form_password”],我会收到以下错误

<b>Notice</b>: Undefined index: form_password in <b>C:\xampp\htdocs\aditya\logout.php</b> on line <b>3</b><br />

团结内的代码

IEnumerator LoginUser(){
        WWWForm form = new WWWForm();
        form.AddField("form_name" , form_name);
        form.AddField("form_password" , form_password );
        form.AddField("unity_hash" , unity_hash);

        WWW login = new WWW(login_url, form);

        yield return login;

        // check for errors
        if (login.error == null)
        {
            Debug.Log(login.text);

        } else {
            Debug.Log("WWW Error: "+ login.error);
        } 
    }

    IEnumerator LogoutUser(){

        WWW logout = new WWW(logout_url);

        yield return logout;

        // check for errors
        if (logout.error == null)
        {
            Debug.Log(logout.text);

        } else {
            Debug.Log("WWW Error: "+ logout.error);
        } 
    }

0 个答案:

没有答案