Home >Backend Development >PHP Tutorial >How to register and read Session in php
Register Session
Using Session Variables in php, in addition to starting, you also need to go through a registration The process of registering and reading Session variables must be completed by accessing the $_SESSION array. Starting from PHP 4.1.0 version, $_SESSION becomes a super global array like $_POST, $_GET and $_COOKIE, etc., but it can only be used after calling session_start() Function to open the Session. Unlike $HTTP_SESSION_VARS, $_SESSION always has global scope, so do not use the global keyword with $_SESSION. Key names in the $_SESSION associative array have the same naming rules as ordinary variable names in PHP.
After session variables are created, they are all stored in the array $_SESSION. Creating a session variable from the array $_SESSION is easy, just add an element directly to the array.
For example, the following example will start the session, create a Session variable and assign it a null value. The code is as follows:
<?php session_start(); // 启动 Session $_SESSION['name'] = null; // 声明一个名为 admin 的变量,并设置为空值 null。 ?>
After executing the script, the Session variable will be saved somewhere on the server side. in a folder. The location of the file is changed through the php.ini file. In the directory specified by the session.save_path attribute, a separate file is created for this access user to save the registered Session variables. For example, the name of a file that saves Session variables is in the form of "sess_09403850rf7sk39s67". The file name contains the Session ID, so each access user has his own file that saves Session variables in the server, and this file can be directly Use a text editor to open. The content structure of the file is as follows:
Variable name | Type: Length: Value //The same structure applies to each variable to save
As mentioned above, let’s give a simple example:
<?php //启动 session session_start(); //注册 session 变量,赋值为一个用户名称 $_SESSION['usermane'] = "sky"; //注册 session 变量,赋值为一个用户id $_SESSION['uid'] = 1; ?>
In the above example, Session has registered two variables. If a file that saves Session variables for this user is found in the server, , after opening, you can see the following content:
username | s:6: "sky"; uid | i:1:"1"; The contents of the two variables
Read the Session
First you need to determine the session variables Whether a session ID exists, if not, create one and make it accessible through the global array $_SESSION; if it already exists, load the created session variable for use by the user. For example: Determine whether the Session variable storing the user name is empty. If not, assign the session variable to $my_value.
The code is shown as follows:
<?php if(!empty($_SESSION['session_name'])){ //判断存储用户名的 Session 会话变量是否为空 $my_value = $_SESSION['session_name']; //将会话变量赋予一个变量 $my_value } ?>
The above is the detailed content of How to register and read Session in php. For more information, please follow other related articles on the PHP Chinese website!