有人可以向我解释会话变量的完整用法吗?

发布于 2024-11-26 23:23:57 字数 110 浏览 0 评论 0原文

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 技术交流群。

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

发布评论

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

评论(3

赠意 2024-12-03 23:23:57

来自 PHP 网站:

PHP 中的会话支持包括一种在后续访问中保留某些数据的方法。这使您能够构建更多定制的应用程序并增加网站的吸引力。所有信息都在会话参考部分中。

来自维基百科:

在计算机科学中,特别是网络中,会话是两个或多个通信设备之间或计算机和用户之间的半永久交互式信息交换,也称为对话、对话或会议(请参阅登录)会议)。会话在某个时间点建立或建立,并在稍后的时间点拆除。已建立的通信会话可能涉及每个方向上的多个消息。会话通常(但并不总是)是有状态的,这意味着至少一个通信部分需要保存有关会话历史记录的信息,以便能够进行通信,这与无状态通信相反,无状态通信由独立的请求组成回应。
已建立的会话是执行面向连接的通信的基本要求。会话也是无连接通信模式中传输的基本步骤。然而,任何单向传输都不定义会话。

基本上,会话用于在您的网站/网络应用程序上(相对)短期停留期间维护用户的状态。他们通过保存一段时间内的登录信息、相关偏好和使用数据等变量来实现这一点。这段时间是用户初始化会话(可能通过登录 ),并销毁他们的会话(可能是由于过期或离开网站)。最终,会话是您作为开发人员选择的任何内容,因为在大多数语言(如 PHP)中,会话本质上只是与给定客户端绑定的变量空间。

示例会话(来自 PHP 网站):

<?php
// page1.php

session_start();

echo 'Welcome to page #1';

$_SESSION['favcolor'] = 'green';
$_SESSION['animal']   = 'cat';
$_SESSION['time']     = time();

// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>

From the PHP website:

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. All information is in the Session reference section.

From Wikipedia:

In computer science, in particular networking, a session is a semi-permanent interactive information interchange, also known as a dialogue, a conversation or a meeting, between two or more communicating devices, or between a computer and user (see Login session). A session is set up or established at a certain point in time, and torn down at a later point in time. An established communication session may involve more than one message in each direction. A session is typically, but not always, stateful, meaning that at least one of the communicating parts needs to save information about the session history in order to be able to communicate, as opposed to stateless communication, where the communication consists of independent requests with responses.
An established session is the basic requirement to perform a connection-oriented communication. A session also is the basic step to transmit in connectionless communication modes. However any unidirectional transmission does not define a session.

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
// page1.php

session_start();

echo 'Welcome to page #1';

$_SESSION['favcolor'] = 'green';
$_SESSION['animal']   = 'cat';
$_SESSION['time']     = time();

// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>
浮华 2024-12-03 23:23:57

使用 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 with some_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.

呆° 2024-12-03 23:23:57

会话是客户端和服务器之间有时间限制的一对一关系的表示。在服务器端语言(例如 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.

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