How to deal with a session in PHP?

Session handling in PHP is a very important concept, which allows users to information remains the same on all pages of the site or application. This article below will take you to learn about the basics of the PHP session handling, we want to help.

What is PHP session (session)?

Session (session) is a mechanism for retaining information on different pages, used to identify users when they visit the website or application.

You must have such a question: why the site needs a session? Before discussing this question, we need to go back and look at how the HTTP protocol works.

HTTP protocol is a stateless protocol, which means that the server can not remember a specific user across multiple requests. For example, when you visit a Web page, the server is only responsible for the content of the page to provide the requested. Therefore, when you visit another page on the same site, Web server will explain each request separately, as if they are independent of each other. The server can not know every request from the same user.

The following paragraphs describe the HTTP protocol.

 

 

In this process, if you want to display information about a particular user, the user must authenticate each request. Imagine if every request, will need to enter a user name and password for authentication on the page; this is too complicated, and simply not practical. And, session (session) at this time comes in handy.

Session (session) allows users across a single site or application of different pages to share information, so it helps to maintain state. This allows the server to know all requests from the same user, thereby allowing the site to display user-specific information and preferences.

The following diagram describes how to use the HTTP protocol with the session.

 

 

How to deal with PHP conversation?

1, start the session

Whenever you want to handle session variables, you need to ensure that the session has started. There are several ways to start a session in PHP.

1) using the function session_start

This is the method most commonly seen, which is initiated by the session session_start function.

 

 

It is important, session_start before sending any output to the browser, you must call the function at the beginning of the script. Otherwise, you will encounter notorious Headers are already sent error.

2), automatic start session

如果需要在整个应用程序中使用会话,还可以选择自动启动会话而不使用session_start函数。

php.ini文件中有一个配置选项session.auto_start,允许我们为每个请求自动启动会话。默认情况下,它设置为0,我们可以将其设置1为启用自动启动功能。

 

 

2、获取会话ID

服务器为每个新会话创建一个唯一的id。如果要获取会话ID,可以使用该session_id功能,如以下代码段所示。

 

 这应该给你当前的会话ID。该session_id函数很有趣,因为它也可以使用一个参数 - 一个会话ID。如果要将系统生成的会话ID替换为您自己的会话ID,可以将其提供给session_id函数的第一个参数。

 

 

重要的是要注意,当您想要使用自定义会话ID启动会话时,必须将session_id函数放在session_start之前调用。

3、创建会话变量

一旦启动会话,$_SESSION就会使用相应的会话信息初始化超全局数组。默认情况下,它使用空白数组初始化,您可以使用键值对存储更多信息。

下面我们通过代码示例来看看如何初始化会话变量。

 

 

如上所示,我们使用session_start函数在脚本开头启动了一个会话;之后,初始化了几个会话变量;最后,我们使用$_SESSION超全局访问了这些变量。

使用$_SESSION超全局将数据存储在会话中时,它最终存储在会话启动时创建的服务器上的相应会话文件中。通过这种方式,会话数据在多个请求之间共享。

正如我们所讨论的,会话信息在请求之间共享,因此在一个页面上初始化的会话变量也可以从其他页面访问,直到会话到期为止。通常,会话在浏览器关闭时到期。

4、修改和删除会话变量

我们可以像修改常规PHP变量一样修改或删除先前在应用程序中创建的会话变量。

下面通过示例来看看如何修改会话变量。

 

 

在上面的脚本中,我们首先检查了是否设置了$_session['count']变量。如果没有设置,我们将设置为1,否则我们将增加1。因此,如果多次刷新此页,可以看到计数器每次递增一个!

另一方面,如果想要删除会话变量,可以使用unset函数,如下面的代码段所示:

 

 

这样,我们就无法再访问$_SESSION[‘logged_in_user_id’]变量了。因为它已被unset函数删除。

5、销毁会话

在上面我们知道可以使用unset函数来删除特定的会话变量;那么如果要一次删除所有与会话相关的数据,我们要怎么办?

其实很简单,我们可以使用session_destroy函数。

下面我们来看看session_destroy函数是如何工作的。

 

 

说明:session_destroy函数删除存储在当前会话中的所有内容。因此,当存储在磁盘上的会话数据被session_destroy函数删除时,我们将从后续请求中看到一个空的会话变量。

注:通常,在用户注销时才会使用session_destroy函数

有需要学习交流的友人请加入交流群的咱们一起,群内都是1-7年的开发者,希望可以一起交流,探讨PHP,swoole这块的技术 或者有其他问题 也可以问,获取swoole或者php进阶相关资料私聊管理即可

别忘了点赞哦,定期分享干货

点此加入该群​jq.qq.com

Guess you like

Origin www.cnblogs.com/heyue0117/p/12149817.html