PHP $_SESSION 同时供多个用户使用 [英] PHP $_SESSION for multiple users at once

查看:112
本文介绍了PHP $_SESSION 同时供多个用户使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 $_SESSION 数组是如何工作的.如果我有很多用户使用我的站点,是否需要为每个用户设置一个子数组?例如现在我有

I'm wondering about how the $_SESSION array works. If I have a lot of users using my site do I need to set a subarray for each user? For instance right now I have

$_SESSION['userid'] = $userid;
$_SESSION['sessionid'] = $sessionid;
$_SESSION['ipaddress'] = $ipaddress;

但是为了应付更多的用户,我需要做一个多维数组吗?

but in order to cope with more users do I need to make a multidimensional array?

$_SESSION[$userid]['sessionid'] = $sessionid;
$_SESSION[$userid]['ipaddress'] = $ipaddress;

$_SESSION 全局是每个客户端处理的还是整体处理的?在登录时设置 $_SESSION['userid'] 是否会将前一个用户踢出并恢复最新登录的用户?

Is the $_SESSION global handled per client or just overall? Will having $_SESSION['userid'] set on login kick the previous user out and instate the latest logged in user?

推荐答案

没有.为每个用户创建了一个单独的 $_SESSION.这一切都由服务器完成,您不必担心.编写代码时,将 $_SESSION 视为网站上只有一个用户.

No. There is a seperate $_SESSION created for each user. This is all done by the server, you don't have to worry about it. When writing your code, treat the $_SESSION as if there was only one user on the site.

实际上,仔细想想,这是一个很好的问题.提出这类问题很好,这意味着您正在认真考虑您的代码如何真正工作.继续问这些事情,并继续测试.我有一种感觉,总有一天你会写出一些惊人的代码.

Actually, on thinking about it, it is a very good question to ask. It is good to ask these sorts of questions, it means you are seriously thinking about how your code truly works. Keep asking these things, and keep testing. I have a feeling that one day you will be writing some amazing code.

因此,请注意,以下是来自 apache 站点的一些信息:

So on that note, here is some info from the apache site:

什么是会话?

会话界面的核心是一个键值对表,可以跨浏览器请求访问.根据使用会话的应用程序的需要,可以将这些对设置为任何有效字符串.

At the core of the session interface is a table of key and value pairs that are made accessible across browser requests. These pairs can be set to any valid string, as needed by the application making use of the session.

在服务器上保持会话

Apache 可以配置为跟踪存储在特定服务器或服务器组上的每个用户会话.此功能类似于典型应用服务器中可用的会话.

Apache can be configured to keep track of per user sessions stored on a particular server or group of servers. This functionality is similar to the sessions available in typical application servers.

如果已配置,将通过使用存储在 cookie 中的会话 ID 或从嵌入在 URL 查询字符串中的参数中提取的会话 ID 来跟踪会话,如典型的 GET 请求.

If configured, sessions are tracked through the use of a session ID that is stored inside a cookie, or extracted from the parameters embedded within the URL query string, as found in a typical GET request.

来自 Sessions 上的 PHP 文档:

And from the PHP docs on Sessions:

PHP 中的会话支持包括一种在后续访问中保留某些数据的方法.这使您能够构建更多自定义应用程序并增加您网站的吸引力.

Session support in PHP consists of a way to preserve certain data across subsequent accesses. This enables you to build more customized applications and increase the appeal of your web site.

访问您网站的访问者会被分配一个唯一的 ID,即所谓的会话 ID.这要么存储在用户端的 cookie 中,要么在 URL 中传播.

A visitor accessing your web site is assigned a unique id, the so-called session id. This is either stored in a cookie on the user side or is propagated in the URL.

会话支持允许您在 $_SESSION 超全局数组中的请求之间存储数据.当访问者访问您的站点时,PHP 将自动检查(如果 session.auto_start 设置为 1)或根据您的请求(通过 session_start() 显式或通过 session_register() 隐式)检查是否已随请求发送了特定的会话 ID.如果是这种情况,则会重新创建先前保存的环境.

The session support allows you to store data between requests in the $_SESSION superglobal array. When a visitor accesses your site, PHP will check automatically (if session.auto_start is set to 1) or on your request (explicitly through session_start() or implicitly through session_register()) whether a specific session id has been sent with the request. If this is the case, the prior saved environment is recreated.

这篇关于PHP $_SESSION 同时供多个用户使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆