分配 $_SESSION 变量

如果我要使用 $_GET 方法获取用户名,如果我有多个用户登录,我会遇到问题。最新登录的用户会覆盖其他用户的信息(不在数据库中),如果前一个用户用户试图谈论他们的用户名将是最新用户的用户名。


前任。用户 Xp10d3 登录。他的用户名仍然是 Xp10d3。用户 IiBlurBeriI 登录。Xp10d3 的用户名突然变成了 IiBlurBeriI 的用户名。


我知道这样做的原因,但我想知道是否要将 $_SESSION 变量分配给 $_GET 变量,该变量会保持静态而不改变吗?如果没有,我该如何解决这个问题?


login_check_update.php:


<?php

    session_start();

    /* Sends an email to the user and adds the special key to another database */

    $username = $_GET['username']; /* Gets the username that was submitted in the HTML form. */

    $password = $_GET['password']; /* Gets the password that was submitted in the HTML form. */

    $servername = "localhost"; /* MySQL database. Change if needed! Most of the time its not localhost unless you're hosting on your computer. */

    $user = 'usernamelol'; /* MySQL username. Change if needed. */

    $pass = 'passwordlol'; /* MySQL password. Change if needed. */

    $dbname = 'vibemcform'; /* MySQL database name. Change if needed. */


    $bytes = random_bytes(10); /* Randomized code */

    $key = bin2hex($bytes); /* Makes the randomized code */


    $link = "live.php";


    $con = new mysqli($servername, $user, $pass, $dbname); /* Connects to the database */

    $query = mysqli_query($con, "SELECT * FROM data WHERE (USERNAME = $username) AND password = $password");

    if (!$query || mysqli_num_rows($query) == 1) {

        echo "Found data in the database! Visit the chat!";

        echo "<form action='live.php' method='post'><a href='".$link."'><input type='submit' name='btn1' value='$username'/></a></form>";

        echo "Session ID: ". session_id() . ". ";

    } else {

        echo "Username not found/password incorrect. Please try again!";

    }


    $conn = null;

    echo 'Username submitted: ' . $username . ' Password submitted: ' . $password . ' .'; exit;

?>

以下代码并不真正相关,因为我主要是复制它,因为我不知道如何进行实时聊天,但我了解其中的 98%:live.php:


<?php

session_start();

$username = $_POST['btn1'];


//Create a session of username and logging in the user to the chat room

if(isset($_POST['username'])){

    $_SESSION['username']=$username;

}


浮云间
浏览 137回答 1
1回答

holdtom

$_SESSION[""] 变量是全局变量。我以前不知道,但我现在知道了。我刚刚为每个注册的用户分配了每个变量。<!DOCTYPE HTML><html><head>&nbsp; &nbsp; <style>&nbsp; &nbsp; &nbsp; &nbsp; body {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text-align: center;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font-family: sans-serif;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; a {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text-decoration: none;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color: blue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; #logout {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; margin: 0 auto;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text-align: center;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; border: 1px solid;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; border-radius: 5px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max-width:1024px;;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height: 800px;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </style></head><body>&nbsp; &nbsp; <div id="logout">&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; session_start();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* Sends an email to the user and adds the special key to another database */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $username = $_GET['username']; /* Gets the username that was submitted in the HTML form. */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $password = $_GET['password']; /* Gets the password that was submitted in the HTML form. */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $email = $_GET['email']; /* Gets the email that was submitted in the HTML form. */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $servername = "localhost"; /* MySQL database. Change if needed! Most of the time its not localhost unless you're hosting on your computer. */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $user = 'xxxx'; /* MySQL username. Change if needed. */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $pass = 'xxxx'; /* MySQL password. Change if needed. */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $dbname = 'vibemcform'; /* MySQL database name. Change if needed. */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $bytes = random_bytes(10); /* Randomized code */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $key = bin2hex($bytes); /* Makes the randomized code */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $con = new mysqli($servername, $user, $pass, $dbname); /* Connects to the database */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query = mysqli_query($con, "SELECT * FROM `data` WHERE USERNAME='".$username."'"); /* Gets the username that was submitted */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $hash = password_hash($password, PASSWORD_DEFAULT);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['hash'] = $hash;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['password_not'] = $password;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (mysqli_num_rows($query) > 0) { /* If the username exists... */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "ERROR: Username already exists. Please try signing up again.";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $con -> close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else { /* If the username DOESN'T exist... */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $conn = new PDO("mysql:host=$servername;dbname=$dbname", $user, $pass);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sql = "INSERT INTO dont (STR, USERNAME, PASSWORD, EMAIL)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VALUES ('$key', '$username', '$hash', '$email')"; /* Insert all the data to the database */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $conn->exec($sql);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch(PDOException $e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $sql . "<br>" . $e->getMessage();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $conn = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $msg = "localhost/vibemcform/verify.php?str=". $key . " Please verify your email!";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $msg = wordwrap($msg,70);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $headers = array("From: xp10d363@gmail.com",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "X-Mailer: PHP/" . PHP_VERSION&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (mail($email,"Verify your email",$msg/*, $headers*/)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo 'Message accepted to your email address! Check your email to verify your account.';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo 'Message not sent to your email. Contact the owner of the website!';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; <a href="index.html">Home</a>&nbsp; &nbsp; </div></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP