会话保持 会话共享_了解PHP会话的工作方式

会话保持 会话共享

开始会议 ( Starting a Session )

php file format

 mmustafabozdemir/Getty Images

mmustafabozdemir / Getty图片

In PHP, a session provides a way to store web page visitor preferences on a web server in the form of variables that can be used across multiple pages. Unlike a cookie, variable information is not stored on the user's computer. The information is retrieved from the web server when a session is opened at the beginning of each web page. The session expires when the web page is closed.

在PHP中,会话提供了一种以可以在多个页面上使用的变量的形式在Web服务器上存储网页访问者首选项的方法。 与cookie不同,变量信息不存储在用户的计算机上。 在每个网页的开头打开会话时,将从Web服务器检索信息。 当网页关闭时,会话终止。

Some information, such as username and authentication credentials, is better saved in cookies because they are needed before the website is accessed. However, sessions offer better security for personal information that is needed after the site launches, and they provide a level of customization for visitors to the site.

最好将某些信息(例如用户名和身份验证凭据)保存在cookie中,因为在访问网站之前需要使用它们。 但是,会话为站点启动后所需的个人信息提供了更好的安全性,并且为站点的访问者提供了一定程度的自定义。

Call this example code mypage.php.

将此示例代码称为mypage.php。

The first thing this example code does is open the session using the session_start() function. It then sets the session variables—color, size, and shape—to be red, small and round respectively.

该示例代码要做的第一件事是使用session_start()函数打开会话。 然后,将会话变量(颜色,大小和形状)分别设置为红色,较小和圆形。

Just as with cookies, the session_start() code must be in the header of the code, and you cannot send anything to the browser before it. It's best to just put it directly after 

与cookie一样,session_start()代码必须位于代码的标题中,并且您不能在此之前向浏览器发送任何内容。 最好直接放在后面

The session sets a tiny cookie on the user's computer to serve as a key. It is only a key; no personal information is included in the cookie. The web server looks for that key when a user enters the URL for one of its hosted websites. If the server finds the key, the session and the information it contains is opened for the first page of the website. If the server does not find the key, the user proceeds to the website, but the information saved on the server is not passed on to the website.

会话在用户的计算机上设置了一个微小的cookie作为密钥。 这只是一个关键; Cookie中不包含任何个人信息。 当用户输入其托管网站之一的URL时,Web服务器将寻找该密钥。 如果服务器找到了密钥,则会为网站的第一页打开会话及其包含的信息。 如果服务器找不到密钥,则用户将转到该网站,但是服务器上保存的信息不会传递到该网站。

使用会话变量 ( Using Session Variables )

Each page on the website that needs access to the information stored in the session must have the  session_start() function listed at the top of the code for that page.  Note that the values for the variables are not specified in the code.

网站上需要访问会话中存储的信息的每个页面都必须在该页面的代码顶部列出session_start()函数。 请注意,变量的值未在代码中指定。

Call this code mypage2.php.

将此代码称为mypage2.php。

All of the values are stored in the $_SESSION array, which is accessed here. Another way to show this is to run this code:

所有值都存储在$ _SESSION数组中,可在此处访问。 另一种显示方式是运行以下代码:

You can also store an array within the session array. Go back to our mypage.php file and edit it slightly to do this:

您也可以在会话数组中存储一个数组。 返回我们的mypage.php文件,并对其进行稍微编辑以执行以下操作:

Now let's run this on mypage2.php to show our new information:

现在,在mypage2.php上运行它以显示我们的新信息:

修改或删除会话 ( Modify or Remove a Session )

This code demonstrates how to edit or remove individual session variables or the entire session. To change a session variable, you just reset it to something else by typing right over it. You can use unset() to remove a single variable or use session_unset() to remove all variables for a session. You can also use session_destroy() to destroy the session completely.

此代码演示了如何编辑或删除单个会话变量或整个会话。 要更改会话变量,只需在其上键入内容即可将其重置为其他变量。 您可以使用unset()删除单个变量,也可以使用session_unset()删除会话的所有变量。 您还可以使用session_destroy()完全破坏会话。

By default, a session lasts until the user closes his browser. This option can be changed in the php.ini file on the web server by changing the 0 in session.cookie_lifetime = 0 to the number of seconds you want the session to last or by using session_set_cookie_params().

默认情况下,会话持续到用户关闭浏览器为止。 通过将session.cookie_lifetime = 0中的0更改为您希望会话持续的秒数,或使用session_set_cookie_params(),可以在Web服务器上的php.ini文件中更改此选项。

翻译自: https://www.thoughtco.com/basic-php-sessions-2693797

会话保持 会话共享

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值