有人可以向我解释会话变量的完整用法吗?
php中的session变量有什么用?我了解基础知识,但我如何判断用户是否在线?会话 ID 是什么?一般来说,我只是使用会话在整个网站中“携带”变量,但仅此而已。还有更多有用的属性吗?
谢谢
What's the use of session variables in php? i know the basics, but how can I tell if a user is online for example? what's the session id? in general, i just use sessions to "carry" a variable throughout my website but that's all. Are there any more useful properties?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自 PHP 网站:
来自维基百科:
基本上,会话用于在您的网站/网络应用程序上(相对)短期停留期间维护用户的状态。他们通过保存一段时间内的登录信息、相关偏好和使用数据等变量来实现这一点。这段时间是用户初始化会话(可能通过登录 ),并销毁他们的会话(可能是由于过期或离开网站)。最终,会话是您作为开发人员选择的任何内容,因为在大多数语言(如 PHP)中,会话本质上只是与给定客户端绑定的变量空间。
示例会话(来自 PHP 网站):
From the PHP website:
From Wikipedia:
Basically, sessions are used to maintain your user's state during a (relatively) short term stay on your website / with your web app. They accomplish this by saving variables such as login information, relevant preferences, and usage data for a certain period of time. This period of time is the amount of time between when a user initializes their session (perhaps by logging in), and destroys their session (perhaps by expiry, or by leaving the website). Ultimately, the session is whatever you as the developer choose to make of it, because in most languages (like PHP) the session is essentially just variable space tied to a given client.
An example session (from the PHP website):
使用 PHP 会话变量可以将一组变量与站点上的每个用户相关联。因此,当您执行
$_SESSION['some_var']
时,您知道您将获得与已分配给该特定访问者的some_var
关联的值。您说您使用它们在访问者访问您网站的整个过程中“携带”变量。这正是它们的用途,您不会错过任何东西。您正在正确地使用它们。
The use of PHP Session variables is so that you can associate a group of variables with every user on the site. So when you do
$_SESSION['some_var']
you know you will get the value associated withsome_var
that has been assigned to that particular visitor.You say you use them to "carry" variables throughout the time a visitor is on your site. That's exactly what they're for, and you're not missing anything. You're using them appropriately.
会话是客户端和服务器之间有时间限制的一对一关系的表示。在服务器端语言(例如 PHP)中,您可以将值附加到此会话。这就是它的范围。您可以通过一百万零一种方式来使用这个概念,或者您也可以只是轻微地依赖它并使用一些其他技术来跟踪持久值。每个会话都有一个唯一的标识符,因此您可以根据需要单独引用 - 这是您的“会话 ID”。
这是一篇文章,介绍了会话的基础知识,但你的问题几乎已经得到了正确的答案 - 会话没有什么神奇的。
如果您必须将其归结为一个类比,请将其想象为一个人在酒店登记入住一晚。当天的日期是会话 ID - 今天只能发生一次。在那家酒店发生的一切都是会议的一部分——当他们去餐厅时,当他们使用制冰机时,等等。当他们早上退房时,会议就结束了。
现在,如果他们再次回来,酒店可能会保留上次住宿的记录(用户状态保存在数据库中),但这是一次新的住宿,因此是一个新的会话。
A session is the representation of a time-limited, one-to-one relationship between client and server. In server-side languages (such as PHP), you can attach values to this session. That's the extent of it. There are a million and one ways you can use this concept, or you can depend on it only lightly and use some other technology for tracking persistent values. Each session has a unique identifier so you can refer to discretely if desired - this is your "session ID".
Here is an article covering the basics of sessions, but you've pretty much got it right in your question - there is nothing magical about sessions.
If you had to boil it down to an analogy, think of it as a person checked into a hotel for a single night's stay. That day's date is the session ID - today can only happen once. Everything that happens in that hotel is part of the session - when they go to the restaurant, when they use the ice machine, etc. When they check out in the morning, the session is over.
Now, if they come back another time, the hotel might have kept a record of the last stay (user state saved in database), but this is a new stay and thus a new session.