繁体   English   中英

分配 $_SESSION 变量

[英]Assigning $_SESSION variables

如果我要使用 $_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;
}

//Unset session and logging out user from the chat room
if(isset($_GET['logout'])){
    unset($_SESSION['username']);
    header('Location:logout.php');
}

?>
<html>
<head>
    <title>Simple Chat Room</title>
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,400,300' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="css/style.css" />
    <script type="text/javascript" src="js/jquery-1.10.2.min.js" ></script>
</head>
<body>
<div class='header'>
    <h1>
        SIMPLE CHAT ROOM
        <?php // Adding the logout link only for logged in users  ?>
        <?php if(isset($_SESSION['username'])) { ?>
            <a class='logout' href="?logout">Logout</a>
        <?php } ?>
    </h1>

</div>

<div class='main'>
<?php //Check if the user is logged in or not ?>
<?php if(isset($_SESSION['username'])) { ?>
<div id='result'></div>
<div class='chatcontrols'>
    <form method="post" onsubmit="return submitchat();">
    <input type='text' name='chat' id='chatbox' autocomplete="off" placeholder="ENTER CHAT HERE" />
    <input type='submit' name='send' id='send' class='btn btn-send' value='Send' />
    <input type='button' name='clear' class='btn btn-clear' id='clear' value='X' title="Clear Chat" />
</form>
<script>
// Javascript function to submit new chat entered by user
function submitchat(){
        if($('#chat').val()=='' || $('#chatbox').val()==' ') return false;
        $.ajax({
            url:'chat.php',
            data:{chat:$('#chatbox').val(),ajaxsend:true},
            method:'post',
            success:function(data){
                $('#result').html(data); // Get the chat records and add it to result div
                $('#chatbox').val(''); //Clear chat box after successful submition
                document.getElementById('result').scrollTop=document.getElementById('result').scrollHeight; // Bring the scrollbar to bottom of the chat resultbox in case of long chatbox
            }
        })
        return false;
};

// Function to continously check the some has submitted any new chat
setInterval(function(){
    $.ajax({
            url:'chat.php',
            data:{ajaxget:true},
            method:'post',
            success:function(data){
                $('#result').html(data);
            }
    })
},1000);

// Function to chat history
$(document).ready(function(){
    $('#clear').click(function(){
        if(!confirm('Are you sure you want to clear chat?'))
            return false;
        $.ajax({
            url:'chat.php',
            data:{username:"<?php echo $_SESSION['username'] ?>",ajaxclear:true},
            method:'post',
            success:function(data){
                $('#result').html(data);
            }
        })
    })
})
</script>
<?php } else { ?>
<div class='userscreen'>
    <form method="post">
        <input type='text' class='input-user' placeholder="ENTER YOUR NAME HERE" name='username' />
        <input type='submit' class='btn btn-user' value='START CHAT' />
    </form>
</div>
<?php } ?>

</div>
</body>
</html>

$_SESSION[""] 变量是全局的。 以前我不知道,但我现在知道了。 我只是为每个注册的用户分配了每个变量。

<!DOCTYPE HTML>
<html>
<head>
    <style>
        body {
            text-align: center;
            font-family: sans-serif;
        }
        a {
            text-decoration: none;
            color: blue;
        }
        #logout {
            margin: 0 auto;
            text-align: center;
            border: 1px solid;
            border-radius: 5px;
            max-width:1024px;;
            height: 800px;
        }
    </style>
</head>
<body>
    <div id="logout">
        <?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. */
            $email = $_GET['email']; /* Gets the email 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 = 'xxxx'; /* MySQL username. Change if needed. */
            $pass = 'xxxx'; /* 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 */

            $con = new mysqli($servername, $user, $pass, $dbname); /* Connects to the database */
            $query = mysqli_query($con, "SELECT * FROM `data` WHERE USERNAME='".$username."'"); /* Gets the username that was submitted */
            $hash = password_hash($password, PASSWORD_DEFAULT);
            $_SESSION['hash'] = $hash;
            $_SESSION['password_not'] = $password;
            if (mysqli_num_rows($query) > 0) { /* If the username exists... */
                    echo "ERROR: Username already exists. Please try signing up again.";
                    $con -> close();
                    exit;
            } else { /* If the username DOESN'T exist... */
                try {
                    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $user, $pass);
                    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                    $sql = "INSERT INTO dont (STR, USERNAME, PASSWORD, EMAIL)
                    VALUES ('$key', '$username', '$hash', '$email')"; /* Insert all the data to the database */
                    $conn->exec($sql);
                }
                catch(PDOException $e) {
                    echo $sql . "<br>" . $e->getMessage();
                }
            }

            $conn = null;
            $msg = "localhost/vibemcform/verify.php?str=". $key . " Please verify your email!";
            $msg = wordwrap($msg,70);
            /*
            $headers = array("From: xp10d363@gmail.com",
                "X-Mailer: PHP/" . PHP_VERSION
            );
            */
            if (mail($email,"Verify your email",$msg/*, $headers*/)) {
                echo 'Message accepted to your email address! Check your email to verify your account.';
            } else {
                echo 'Message not sent to your email. Contact the owner of the website!';
            }
            exit;
        ?>

        <a href="index.html">Home</a>
    </div>
</body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM