简体   繁体   English

在 php 语言的登录任务中使用 session

[英]Using session in login task in php language

Using SESSION to login with one tab and open other tab, copy URL in first tab and paste it to second tab - for example (crud operation) in two tabs and update the result after refreshing.使用 SESSION 使用一个选项卡登录并打开另一个选项卡,在第一个选项卡中复制 URL 并将其粘贴到第二个选项卡 - 例如(粗操作)在两个选项卡中并在刷新后更新结果。 At this logged in moment, what if anyone using other pcs to access my website by other browsers with same URL?在这个登录的时刻,如果有人使用其他电脑通过其他具有相同 URL 的浏览器访问我的网站怎么办?

This is my code:这是我的代码:

login.php
    <?php    
        require_once "navigation.php";
        require_once "connection_to_database.php";    
        session_start();
        // use mysqli to connect and fetch data from database    
        $method = filter_input(INPUT_SERVER, "REQUEST_METHOD");
        $action = filter_input(INPUT_POST, "action");
        $field = filter_input(INPUT_POST, "field");    
        $page = "";
        if ($method == "POST") {
            if ($action == "login") {
                // check field is empty, same with pattern or not
                // escape value
                // compare with database value, set $_SESSION["username"]
                // navigate to user index page with this $_SESSION["username"]
                // display $_SESSION["username"]
                $page = "/user_index.php";
            }
        }
        forwardToPage($page);
    ?>
user_index.php
    <?php
        require_once "navigation.php";
        if (session_status() == PHP_SESSION_NONE || session_id() == "") {
            session_start();
            if (!(isset($_SESSION["username"]))) {
                forwardToPage("/login.php");
            }
        }
    ?>
    <!DOCTYPE html>
    <html>
        <head>
            ...
        </head>
        <body>
            ...
            <span>
                welcome <form action="/logout.php" method="post"><input type="submit" value="<?php echo $_SESSION["username"]; ?>" /></form>
            </span>
            ...
        </body>
    </html>

I will appreciate if anyone can share knowledge or sample codes with me.如果有人可以与我分享知识或示例代码,我将不胜感激。 Thank you.谢谢你。

At the time you login to the website, the session ID will be send back and save to your browser cookies.在您登录网站时,session ID 将被发送回并保存到您的浏览器 cookies。 And If someone open the same url, they don't have your session ID so they cannot behave as you in that website.如果有人打开相同的 url,他们没有您的 session ID,因此他们无法在该网站上像您一样行事。

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

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