Table of Contents
1. What is session control
2. Why do you need session control?
3. The principle and function of Cookie
4. How to use Cooike (general situation)
5. The principle and function of Session
6. How to use Session
Home Backend Development PHP Tutorial php - session control

php - session control

Jul 29, 2016 am 09:01 AM
cookie key nbsp session setcookie

1. What is session control

Allows the server to make continuous requests based on the client.

2. Why do you need session control?

Because when you open a website and want to access other pages of the website, if there is no session control, you will need to enter your account and password again when you jump to other pages.

Save the client’s simple information in the personal PC, and other programs obtain the PC’s Cookie to obtain the user’s information. This way there is no need for the user to enter the account and password themselves

Note: setCookie() must be used before php outputs the first sentence, otherwise it will be invalid

4. How to use Cooike (general situation)

Create Cookie

<span>setCookie</span>("key","value",retainTime);<span>//</span><span>创建Cookie</span>
Copy after login

Call Cookie

<span>if</span> (<span>$_COOKIE</span>["key"] == "admin"<span>){ <span>//Cookie是php提供的超级数组
  </span></span><span>echo</span> "获取数据成功"<span>;       
}</span>
Copy after login

Delete Cookie

<span>//</span><span>第一种方法</span><span>setCookie</span>("key");<span>//</span><span>只需要输入键名即可
//第二种方法</span><span>setCookie</span>("key","",<span>time</span>()-1000);<span>//</span><span>让保留的时间小于当前时间</span>
Copy after login

Cookie supports becoming a multi-dimensional array

<span>setCookie</span>("user[key]","values"); <span>//</span><span>相当于$_COOKIE["user"]["key"]</span>
Copy after login


Simple example: Cookie-based user login

5. The principle and function of Session

Information is stored on the server rather than on individual PCs.

6. How to use Session

(1). Configure php.ini options (do not expand, check the relevant documents yourself)

(2). Start session

<span>session_start</span>();<span>//</span><span>在使用session之前都必须先调用该方法</span>
Copy after login

Function: Configure the built-in environment related to Session Variables are preloaded into memory.

(3) Call

<span>$_SESSION</span>["key"] = "value";<span>//</span><span>$_SESSION也是超级数组,并以数组方式调用</span>
Copy after login

(4) Delete

<span>//</span><span>单个删除</span><span>unset</span>(<span>$_SESSION</span>["key"<span>]);
</span><span>//</span><span>全部删除</span><span>$_SESSION</span> = <span>array</span>(); <span>//</span><span>设置成空数组
//将这个用户在服务器端对应的Session文件删除</span>session_destory();
Copy after login

The above introduces php - session control, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1243
24
10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

How to turn off private browsing authentication for iPhone in Safari? How to turn off private browsing authentication for iPhone in Safari? Nov 29, 2023 pm 11:21 PM

In iOS 17, Apple introduced several new privacy and security features to its mobile operating system, one of which is the ability to require two-step authentication for private browsing tabs in Safari. Here's how it works and how to turn it off. On an iPhone or iPad running iOS 17 or iPadOS 17, Apple's browser now requires Face ID/Touch ID authentication or a passcode if you have any Private Browsing tab open in Safari and then exit the session or app to access them again. In other words, if someone gets their hands on your iPhone or iPad while it's unlocked, they still won't be able to view your privacy without knowing your passcode

Win10/11 digital activation script MAS version 2.2 re-supports digital activation Win10/11 digital activation script MAS version 2.2 re-supports digital activation Oct 16, 2023 am 08:13 AM

The famous activation script MAS2.2 version supports digital activation again. The method originated from @asdcorp and the team. The MAS author calls it HWID2. Download gatherosstate.exe (not original, modified) from https://github.com/massgravel/Microsoft-Activation-Scripts, run it with parameters, and generate GenuineTicket.xml. First take a look at the original method: gatherosstate.exePfn=xxxxxxx;DownlevelGenuineState=1 and then compare with the latest method: gatheros

Where are cookies stored? Where are cookies stored? Dec 20, 2023 pm 03:07 PM

Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

What does the identity attribute in SQL mean? What does the identity attribute in SQL mean? Feb 19, 2024 am 11:24 AM

What is Identity in SQL? Specific code examples are needed. In SQL, Identity is a special data type used to generate auto-incrementing numbers. It is often used to uniquely identify each row of data in a table. The Identity column is often used in conjunction with the primary key column to ensure that each record has a unique identifier. This article will detail how to use Identity and some practical code examples. The basic way to use Identity is to use Identit when creating a table.

Where are the cookies on your computer? Where are the cookies on your computer? Dec 22, 2023 pm 03:46 PM

Cookies on your computer are stored in specific locations on your browser, depending on the browser and operating system used: 1. Google Chrome, stored in C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default \Cookies etc.

How to solve session failure How to solve session failure Oct 18, 2023 pm 05:19 PM

Session failure is usually caused by the session lifetime expiration or server shutdown. The solutions: 1. Extend the lifetime of the session; 2. Use persistent storage; 3. Use cookies; 4. Update the session asynchronously; 5. Use session management middleware.

Solution to PHP Session cross-domain problem Solution to PHP Session cross-domain problem Oct 12, 2023 pm 03:00 PM

Solution to the cross-domain problem of PHPSession In the development of front-end and back-end separation, cross-domain requests have become the norm. When dealing with cross-domain issues, we usually involve the use and management of sessions. However, due to browser origin policy restrictions, sessions cannot be shared by default across domains. In order to solve this problem, we need to use some techniques and methods to achieve cross-domain sharing of sessions. 1. The most common use of cookies to share sessions across domains

See all articles