Home > Backend Development > PHP Tutorial > PHP Session Development Principles and Detailed Usage

PHP Session Development Principles and Detailed Usage

小云云
Release: 2023-03-22 14:42:01
Original
3253 people have browsed it

PHP Session Development Principles and Detailed Usage

Store a user's information, change the user session settings, and all pages can be used. This article mainly shares with you the principles of PHP Session development and detailed usage instructions. I hope it can help you.

PHP Session variable

Open the program, make some changes, then save and exit. It's just a session. The server can know who is operating through the session.
Working mechanism: Create a uid for the visitor, store variables according to this uid, the uid is stored in a cookie, or URL transmission.

Related topic recommendations: php session (including pictures, texts, videos, cases)

Use PHP Session

Start the session before storing user information in the session
Note: The session_start() function must be located before the <html> tag:

<?php
 session_start(); 
 ?><html><body></body></html>
Copy after login
Copy after login

Register the user session with the server , convenient for saving user information, and assigning a uid to the user session

Storage Session variable

To store and obtain session, use $_SESSION

<?phpsession_start();// 存储 session 数据if(isset($_SESSION[&#39;views&#39;]))
{    $_SESSION[&#39;views&#39;]=$_SESSION[&#39;views&#39;]+1;
}else{    $_SESSION[&#39;views&#39;]=9999;
}?><html><head><meta charset="utf-8"></head><body><?php// 检索 session 数据echo "浏览量:". $_SESSION[&#39;views&#39;];?></body></html>
Copy after login
Copy after login

Output "Views: 9999"

Destroy Session

Which unset() or session_destroy() function is used to destroy

<?phpsession_start();if(isset($_SESSION[&#39;views&#39;]))
{    //释放指定的 session 变量
    unset($_SESSION[&#39;views&#39;]);
}//彻底销毁 sessionsession_destroy();?>
Copy after login
Copy after login

Function: Store a user's information, change user session settings, and can be used on all pages.

PHP Session variable

Open the program, make some changes, then save and exit. It's just a session. The server can know who is operating through the session.
Working mechanism: Create a uid for the visitor, store variables according to this uid, the uid is stored in a cookie, or URL transmission.

Use PHP Session

Save user information before the session and start the session
Note: The session_start() function must be located before the <html> tag:

<?php
 session_start(); 
 ?><html><body></body></html>
Copy after login
Copy after login

Register the user session with the server to facilitate saving user information, and assign a uid to the user session

Storage Session variable

Store and obtain session using $ _SESSION

<?phpsession_start();// 存储 session 数据if(isset($_SESSION[&#39;views&#39;]))
{    $_SESSION[&#39;views&#39;]=$_SESSION[&#39;views&#39;]+1;
}else{    $_SESSION[&#39;views&#39;]=9999;
}?><html><head><meta charset="utf-8"></head><body><?php// 检索 session 数据echo "浏览量:". $_SESSION[&#39;views&#39;];?></body></html>
Copy after login
Copy after login

Output "Views: 9999"

Destroy Session

Which one to useunset() or session_destroy() Function destruction

<?phpsession_start();if(isset($_SESSION[&#39;views&#39;]))
{    //释放指定的 session 变量
    unset($_SESSION[&#39;views&#39;]);
}//彻底销毁 sessionsession_destroy();?>
Copy after login
Copy after login

Related recommendations:

PHP Session principle analysis and use

Analysis of SESSION principle in PHP and large websites Application Notes

Session Principle Brief Description_PHP Tutorial

The above is the detailed content of PHP Session Development Principles and Detailed Usage. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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