如何在不同的电脑上访问php会​​话

发布于 2024-11-03 09:18:37 字数 762 浏览 1 评论 0原文

我正在做一些关于在 php 中使用会话的练习。我是这样设置的:

$_SESSION['log_users'][] = array(array('Username'=>$username))

我尝试对其进行实验。并发现当我使用不同的 IP 访问它时,生成的会话是不同的。同时使用相同的浏览器,即firefox。

我所做的如下:

  1. 设置我的路由器,以便其他人能够通过使用我的外部 IP 地址访问我正在进行的练习。
  2. 然后我打开了练习的本地主机版本:

    http://localhost/exercise/sessions.php

  3. 然后使用外部IP地址:

    http://201.xxx.xxx/exercise/sessions.php< /p>

  4. 然后我在每个浏览器选项卡上填充会话数组。并发现这两个中的每一个都保留了不同版本的会话。通过使用 print_r($_SESSION['log_users'])

    发现,

这真的是它应该表现的方式吗?我可以做些什么以使会话只有一个版本吗?我目前使用的是 Wampserver 2.1

I'm doing some exercises on using sessions in php. I set it up this way:

$_SESSION['log_users'][] = array(array('Username'=>$username))

I tried to experiment on it. And found out that the session that is being generated is different when I use a different ip for accessing it. While using the same browser, which is firefox.

Here is what I did:

  1. Setup my router so that others will be able to access the exercise that I'm working on through the use of my external ip address.
  2. I then opened the localhost version of the exercise:

    http://localhost/exercise/sessions.php

  3. Then the one using the external ip address:

    http://201.xxx.xxx/exercise/sessions.php

  4. I then filled up the session array on each browser tab. And found out that each of those two keeps a different version of the session. Found out by using print_r($_SESSION['log_users'])

Is this really the way it should behave? Is there anything I can do so that there's only one version of the session? I'm currently using Wampserver 2.1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鹿童谣 2024-11-10 09:18:37

会话存储在服务器端,并在客户端创建会话cookie,以识别保存当前会话id的浏览器的当前会话。

会话 cookie 是根据您用于访问站点的域来存储的。

由于您使用不同的域,一个是 localhost ,另一个是 ip ,这将创建两个不同的会话。

当您通过 localhost 域访问页面时。它将创建会话并在域 localhost 上存储会话 cookie。如果您访问同一域系统上的另一个页面,将检查会话 cookie 是否存在,它会恢复旧会话并且不会创建新会话。

同时,如果您通过 ip 访问,则不会为该 ip 存储会话 cookie,但系统会假设该用户没有活动会话,并将启动一个新会话会话和会话 cookie 是基于此 ip 存储的。

这就是会话的工作方式。

希望这有帮助。

The session is stored on server side and a session cookie is created on client side to identify the current session of browser which holds current session id.

The session cookie is stored based on the domain you are using to access the site.

Since you are using different domain one is localhost and another is ip which will create two different sessions.

When you visit pages through localhost domain. It will create session and store session cookie on the domain localhost. If you visit another page on same domain system will check if the session cookie exists it resume the old session and does not create new one.

While the same time if you access through ip the session cookie is not stored for this ip yet then system assume that there is no active session for this user and will start a new session and session cookie is stored for based on this ip.

This is the way how session works.

Hope this helps.

不语却知心 2024-11-10 09:18:37

会话cookie与域名绑定。当您第一次访问它时,它将绑定到localhost域。

如果您随后将浏览器指向 201.xx.xx.xx 地址,则该域名将不再匹配。并且您的浏览器将不会再次发送此 cookie。这就是为什么会生成新会话的原因。即使它实际上是同一台服务器。

The session cookie is bound to a domain name. When you first access it, it will be bound to the localhost domain.

If you then point your browser to the 201.xx.xx.xx address, the domain name will no longer match. And your browser will not send this cookie again. This is why a new session will be generated. Even though it is factually the same server.

孤独患者 2024-11-10 09:18:37

在你的情况下,只有 $_SESSION 对你没有帮助。您还应该尝试使用 $_SESSION 和数据库。

您应该同步您的会话和数据库会话记录。

系统将检查您在数据库中的条目。如果您有可用的条目,那么它将直接为您的站点生成会话。这样,所有浏览器只能使用一次登录。

In your case only $_SESSION will not be helpful for you. You should try with $_SESSION and Database also.

You should synchronize your session and database session record.

System will check your entry in database. If you have entry available then it will directly generate session for your site. This way only one login can be available for all browsers.

凹づ凸ル 2024-11-10 09:18:37

虽然 Shakti Singh 的答案在技术上可能是正确的。
在我看来,你似乎试图实现的目标是无法实现的。

使用 session_id() 加数据库的方式可能大致如下:

  • 启动会话,
  • 将 session_id 与用户名(在数据库中)联系起来,
From my checkpass.php
...

// Connects to your Database

...

    session_start();
    $sql = 'SELECT * FROM `login` WHERE `user`="xxUserxx"';
    $result = $db->query($sql);
    $row = $result->fetch_row();
        if ($_POST['passwort'] == $row[0])
        {
            if (!$db -> query('UPDATE `login` SET `sessionid`="'.session_id().'" WHERE `user`="xxUserxx"'))
            {
                die('UPDATE `login` SET `sessionid`="'.session_id().'" WHERE `user`="xxUserxx"<br>Was not able to create session in database! '.$db->error);
            }
            header('Location: backtothecalling.php');
            exit;
        }
        else
        {
            ?>
            <form action="" method="post">
                <input type="password" size="13"  maxlength="13" name="passwort" autofocus=TRUE required>
                <input type="submit" value="login">
            </form>
            <?php
        }

  • 定期检查此登录是否仍然有效。

// Connects to your Database

...

        //Already logged in? ...
        session_start(); #Starts or continues a session. this gives you a session id.
        $sql = 'SELECT * FROM `login` WHERE `user`="xxUserxx"';
        if (!$result = $db->query($sql))
        {
            die("Couldn't get user data from database. Message: ".$db->error);
        }
        $row = $result->fetch_row(); #In $row[1] is now the session_id from the last successful login.
        if (session_id() != $row[1]) #Check for valid login (compare actual session_id with the one in db).
        {
                                                #If not already logged in, checkpass.php gives a
                                                #login screen and asks for login factors,
            header ('Location: checkpass.php'); #and will store valid session_id in db.
            exit;
        }
    #From here on you are correctly logged in, continue with whatever you want to do here with the user (in my case there is only one user, so keep that in mind if you miss username checks in my code).

...

While the answer from Shakti Singh may be technically correct.
The goal you seem to be trying to achieve is imo not reachable that way.

The way with session_id() plus database may look roughly like:

  • start session
  • tie session_id to username (in database)
From my checkpass.php
...

// Connects to your Database

...

    session_start();
    $sql = 'SELECT * FROM `login` WHERE `user`="xxUserxx"';
    $result = $db->query($sql);
    $row = $result->fetch_row();
        if ($_POST['passwort'] == $row[0])
        {
            if (!$db -> query('UPDATE `login` SET `sessionid`="'.session_id().'" WHERE `user`="xxUserxx"'))
            {
                die('UPDATE `login` SET `sessionid`="'.session_id().'" WHERE `user`="xxUserxx"<br>Was not able to create session in database! '.$db->error);
            }
            header('Location: backtothecalling.php');
            exit;
        }
        else
        {
            ?>
            <form action="" method="post">
                <input type="password" size="13"  maxlength="13" name="passwort" autofocus=TRUE required>
                <input type="submit" value="login">
            </form>
            <?php
        }

  • check regularly if this login is still active and valid.

// Connects to your Database

...

        //Already logged in? ...
        session_start(); #Starts or continues a session. this gives you a session id.
        $sql = 'SELECT * FROM `login` WHERE `user`="xxUserxx"';
        if (!$result = $db->query($sql))
        {
            die("Couldn't get user data from database. Message: ".$db->error);
        }
        $row = $result->fetch_row(); #In $row[1] is now the session_id from the last successful login.
        if (session_id() != $row[1]) #Check for valid login (compare actual session_id with the one in db).
        {
                                                #If not already logged in, checkpass.php gives a
                                                #login screen and asks for login factors,
            header ('Location: checkpass.php'); #and will store valid session_id in db.
            exit;
        }
    #From here on you are correctly logged in, continue with whatever you want to do here with the user (in my case there is only one user, so keep that in mind if you miss username checks in my code).

...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文