dousi4900 2016-04-28 19:04
浏览 98

PHP session_id()被覆盖

I really don't know how to describes this, but here goes. I'm trying to make a Login for an online multiplayer game (cluedo to be exact) that is written using mainly the LAMP stack (php,js,etc), and creating a session when someone logs in/registers works for me and all and I even save it to a sql database, but it seems that as soon as a next user Logs in/registers the session id to the previous user is overridden to the new ones details. In other words they both now have the same session for some reason.

My Game LogIn basically:

    <!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>

and the lobby (where I wait for someone to press game start or for more people to register)

<?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>";


        }
    }

 ?>

and then the Display.js runs in a loop until either 6 people connect or a user presses start. It also displays everyone currently waiting for a game by outputting the SQL database, but I don't think that code is necessary to add, but then when that's done I go to the Cluedo.php page and if I echo session_id() there on two different browsers(same machine) I get the same 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>

Also I'm using XAMPP and running it all locally at the moment so don't know if there's a problem there maybe?

Here's an example of my database with the saved values, the table saves unique sessions however when the session_id() is echoed at the Cluedo.php page it is always the last registered user:

UserTable

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 对于squad数据集的基于bert模型的微调
    • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
    • ¥20 steam下载游戏占用内存
    • ¥15 CST保存项目时失败
    • ¥15 树莓派5怎么用camera module 3啊
    • ¥20 java在应用程序里获取不到扬声器设备
    • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
    • ¥15 Attention is all you need 的代码运行
    • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
    • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: