PHP session_id()被覆盖

时间:2016-04-28 19:04:12

标签: php session

我真的不知道如何描述这个,但是这里有。我正在尝试登录一个在线多人游戏(确切地说是cluedo),主要使用LAMP堆栈(php,js等)编写,并在有人登录/注册时为我和所有人创建会话我甚至将它保存到一个sql数据库,但似乎只要下一个用户登录/注册会话ID到前一个用户被覆盖到新的详细信息。换句话说,由于某种原因,他们现在都有相同的会话。

我的游戏登录基本上:

    <!DOCTYPE html>
    <html>
        <head>
            <link rel="stylesheet" type="text/css" href="StyleSheet.css">
            <meta charset="UTF-8">
            <title>Game Setup</title>
        </head>

     <body>
         <header>
             <h1>Game Setup</h1>
         </header>



//My form for user details and character select
         <form name="Registration" id=Form1 action="Lobby.php" method="POST">
         <fieldset>
             <legend>Player information:</legend>
             <label for="Pid">Player Name(required): </label><br>
             <input autofocus type="text" id="Pid" name="Pid" placeholder="Player ID" required><br>
             <label for="Avatar">Character(required):</label><br>
             <select name="Avatar">
                <option value="Miss White">Miss White</option>
                <option value="Col Mustard">Col Mustard</option>
                <option value="Miss Scarlet">Miss Scarlet</option>
                <option value="Mr Green">Mr Green</option>
                <option value="Madame Plum">Madame Plum</option>
                <option value="Benjamin Blue">Benjamin Blue</option>
            </select><br><br>
             <input type="submit" value="Register">
           </fieldset>  
         </form>

     </body>

和大厅(我等待有人按游戏开始或者让更多人注册)

<?php 
    session_start() ;
    session_regenerate_id();
    $_SESSION["UserName"]=$_POST['Pid'];
    $_SESSION["Avatar"]=$_POST['Avatar'];
 ?>
 <?php require 'DatabaseConnect.php' ?>
 <?php include 'Users.php' ?>

<?php
$PlayerID = mysqli_real_escape_string($conn, $_POST['Pid']);
$PlayerChar = mysqli_real_escape_string($conn, $_POST['Avatar']);
$SesID = session_id();
    $sql = "INSERT INTO users (UserName, Avatar, sessionID)
    VALUES ('$PlayerID', '$PlayerChar','$SesID')";

    if ($conn->query($sql) === TRUE) {
        echo "Welcome $PlayerID , currently in Lobby: <br>";

        $User = new Users();
        $User->setUserName($_SESSION['UserName']);
        $User->setAvatar( $_SESSION['Avatar']);
        $User->isPlaying = false;
        $_SESSION['User'] = serialize($User);

        ?>
        <html>
            <body>
        <div id="Players"> </div>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
        <script src ="Display.js"></script>
            </body>
         </html>

       <?php 

        }
        else {
        if($conn->errno == 1062){//when a userName or Character already in use
             $sql = "SELECT * FROM users";
             $result = $conn->query($sql);
             $sql = "ALTER TABLE users AUTO_INCREMENT = ".$result->num_rows;
             $conn->query($sql);

            echo "<script type='text/javascript'>alert('ERROR :Username or Character already in use!')</script>";
           echo" <script>
                window.location = 'LogIn.php';
                </script>";


        }
    }

 ?>

然后Display.js循环运行,直到6个人连接或用户按下start。它还通过输出SQL数据库显示当前正在等待游戏的每个人,但是我不认为添加代码是必要的,但是当完成时我会转到Cluedo.php页面并且如果我在那里回显session_id()两个不同的浏览器(同一台机器)我得到相同的session_id()

 <?php 
    session_start() ;

 ?>
 <?php require 'DatabaseConnect.php' ?>
 <?php include 'Users.php' ?>

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="StyleSheet.css">
        <meta charset="UTF-8">
        <title>Cluedo</title>
    </head>

 <body>
        <?php 
                 $User = unserialize($_SESSION['User']);
                 echo session_id();
        ?>
 </body>

 </html>

此外我正在使用XAMPP并且此刻在本地运行它所以不知道是否存在问题?

以下是我的数据库中包含已保存值的示例,该表保存了唯一的会话,但是当在Cluedo.php页面上回显session_id()时,它始终是最后一个注册用户:

UserTable

0 个答案:

没有答案