Home > Backend Development > PHP Tutorial > How to register and read Session in php

How to register and read Session in php

伊谢尔伦
Release: 2023-03-07 16:42:01
Original
3316 people have browsed it

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[&#39;name&#39;] = null;                   // 声明一个名为 admin 的变量,并设置为空值 null。
?>
Copy after login

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[&#39;usermane&#39;] = "sky";
  //注册 session 变量,赋值为一个用户id
  $_SESSION[&#39;uid&#39;] = 1;
?>
Copy after login

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[&#39;session_name&#39;])){       //判断存储用户名的  Session 会话变量是否为空
    $my_value = $_SESSION[&#39;session_name&#39;];     //将会话变量赋予一个变量 $my_value
  }
?>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template