PHP session_start()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4015729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:44:20  来源:igfitidea点击:

PHP session_start()

phpsession

提问by Scott

What is this really?

这真的是什么?

Does it start a current session based on cookies? Got that from the PHP website. How does PHP control the session? If I start a session when a user opens up my login page, what do I even use that session for? Can I use the current session to get info about the logged in user?

它是否基于 cookie 启动当前会话?从 PHP 网站上得到的。PHP 如何控制会话?如果我在用户打开我的登录页面时开始会话,我什至使用该会话做什么?我可以使用当前会话来获取有关登录用户的信息吗?

回答by Victor Nicollet

The PHP session system lets you store securely data in the $_SESSIONglobal array. A typical example is to store the user's identifier in the session when they type in their password:

PHP 会话系统允许您将数据安全地存储在$_SESSION全局数组中。一个典型的例子是在用户输入密码时将用户的标识符存储在会话中:

if ($user = try_login($login, $password)) 
  $_SESSION['user'] = $user;

Then, you can access that information on all other pages:

然后,您可以在所有其他页面上访问该信息:

if (isset($_SESSION['user']))
  // logged in !
  echo user_name($_SESSION['user']);

The data is stored on the server, so there is no risk of tampering (on the other hand, mind your disk usage).

数据存储在服务器上,因此不存在篡改的风险(另一方面,请注意您的磁盘使用情况)。

Starting the session lets the current request use $_SESSION. If this is the user's first visit, the array will be empty and a new session cookie will be sent for you.

启动会话让当前请求使用$_SESSION. 如果这是用户的第一次访问,该数组将为空,并且会为您发送一个新的会话 cookie。

Closing the session merely prevents the current request from using $_SESSION, but the data stays around for the next requests.

关闭会话只会阻止当前请求使用$_SESSION,但数据会保留用于下一个请求。

Destroying the session throws away all the data, forever. The sessions are destroyed a certain duration after the last visit (usually around 30 minutes).

销毁会话会永远丢弃所有数据。会话在最后一次访问后的一段时间内被销毁(通常大约 30 分钟)。

回答by Manos Dilaverakis

I assume you want to know what a PHP session means for you, the programmer.

我假设您想知道 PHP 会话对您(程序员)意味着什么。

When you do session_start() you are telling PHP that you want to use the session. This is made available to you as an array called $_SESSION. You can use that like any other array with the difference that the stuff you put in there stays there from one page to another (provided you use session_start() at the beginning of each page).

当您执行 session_start() 时,您是在告诉 PHP 您要使用会话。它以名为 $_SESSION 的数组形式提供给您。您可以像使用任何其他数组一样使用它,不同之处在于您放入其中的内容从一页到另一页都保留在那里(前提是您在每页的开头使用 session_start())。

The actual mechanism may vary depending on configuration (php.ini), but a typical installation can use cookies for the session. Let's assume that your webserver is on linux and you're using cookies. You do the following

实际机制可能因配置(php.ini)而异,但典型的安装可以为会话使用 cookie。假设您的网络服务器在 linux 上并且您正在使用 cookie。您执行以下操作

session_start();
$_SESSION['name']='Bob';

When PHP sees this it creates a text file with a semi-random name (for example sess_a3tfkd5558kf5rlm44i538fj07), sticks the $_SESSION contents in there as plain text and then sends a cookie to the user with the session id, which can be used to find the session file (for example a3tfkd5558kf5rlm44i538fj07).

当 PHP 看到它时,它会创建一个具有半随机名称的文本文件(例如 sess_a3tfkd5558kf5rlm44i538fj07),将 $_SESSION 内容作为纯文本粘贴在其中,然后使用会话 ID 向用户发送 cookie,可用于查找会话文件(例如 a3tfkd5558kf5rlm44i538fj07)。

The next time the user comes back he hands in the session id in his cookie, PHP goes to the relevant file and loads its contents in $_SESSION.

下次用户回来时,他在他的 cookie 中提交会话 ID,PHP 转到相关文件并将其内容加载到 $_SESSION 中。

You'll note that the actual information is kept on the server while the user is only given an id. Kinda like handing in your coat in a club and getting a ticket with a number on it.

您会注意到实际信息保存在服务器上,而用户只得到一个 id。有点像在俱乐部里交上你的外套,然后拿到一张上面有号码的票。

回答by Charlie Collins

PHP's session_start starts OR resumes an HTTP session, which is explained fairly well in this article:

PHP 的 session_start 启动或恢复 HTTP 会话,本文对此进行了很好的解释:

http://en.wikipedia.org/wiki/Session_(computer_science)

http://en.wikipedia.org/wiki/Session_(computer_science)

The concept of an HTTP "session" isn't specific to PHP, it's used in many (all?) server side HTTP frameworks as one way to allow for some state to be stored/associated across different request/responses (since HTTP is stateless). A unique token (which is often, but not always, stored in a cookie) identifies a particular client, and the server can associate the "session."

HTTP“会话”的概念并非特定于 PHP,它在许多(所有?)服务器端 HTTP 框架中都被用作允许跨不同请求/响应存储/关联某些状态的一种方式(因为 HTTP 是无状态的)。唯一的令牌(通常但不总是存储在 cookie 中)标识特定客户端,服务器可以关联“会话”。

Here's some more info about sessions and PHP in particular that may help: http://www.php.net/manual/en/book.session.php

这里有一些关于会话和 PHP 的更多信息,特别是可能会有所帮助:http: //www.php.net/manual/en/book.session.php

回答by Gordon

Like it says in the Manual

就像手册上说的

session_start()creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.

session_start()根据通过 GET 或 POST 请求或通过 cookie 传递的会话标识符创建会话或恢复当前会话。

If you start a new session at your login page, the session is initially empty. You can store in it whatever you want, for instance, store the user id once the user has logged in. The session data is destroyed when you close the session.

如果您在登录页面开始一个新会话,该会话最初是空的。您可以在其中存储任何您想要的内容,例如,在用户登录后存储用户 ID。当您关闭会话时,会话数据将被销毁。

You might want to read all chapters in the Session Extension Manual Pagesand also see

您可能希望阅读Session Extension Manual Pages中的所有章节,并查看

回答by Faruque Ahamed Mollick

You can compare PHP session with the cookie, but session is the much more secure way of storing information. Cookie store data on user's computer, but session store on the server in a temporary file securely. I have discussed session and how to use it on one of my blog post - How to start a PHP session, store and accessing Session data?

您可以将 PHP 会话与 cookie 进行比较,但会话是一种更安全的信息存储方式。Cookie 将数据存储在用户计算机上,但会话安全地存储在服务器上的临时文件中。我在我的一篇博文中讨论了会话以及如何使用它 -如何启动 PHP 会话、存储和访问会话数据?

Below is an example code of storing data in PHP session:

下面是在 PHP 会话中存储数据的示例代码:

<?php
session_start();
$_SESSION["name"] = "John";
?>

Below is the example of retriving the session data:

以下是检索会话数据的示例:

<?php
session_start();
echo $_SESSION["name"];
?>

The above code will display the name "John".

上面的代码将显示名称“John”。

Source: How to start a PHP session, store and accessing Session data?

来源:如何启动一个 PHP 会话,存储和访问会话数据?