Home >Backend Development >PHP Tutorial >PHP became a monk halfway (4)_PHP tutorial
Previous article: http://www.BkJia.com/kf/201205/131555.html
Due to the failure in writing a BLOG system last time, I need to understand some basic mechanisms
Let’s start with Cookie and Session!
Let’s ask in layman’s terms: What are Cookie and Session?
Answer: Storage mechanism!!!
Cookie: Data transfer method from Web page -> Web page, Client
Session: A method to ensure that data remains valid in the page, exists in Server.
Session mechanism --- can track users according to a session in the website
Can support user login and display information according to user preferences
PHP session---driven by a unique session ID.
This ID is an encrypted random number
Will be saved on the client during the life cycle of the session
Saved in Cookie and spread online through URL
Let’s talk about COOKIE first
Overview:
A mechanism to store data on the remote browser side and use it to identify and track users
It will be stored on the user’s hard drive, and specific information will be read when the user logs in again
Format: username@website address[number].txt
Function:
Record certain information about visitors
Passing variables between pages
Save the viewed page in the Cooike folder to improve the speed of re-visiting.
Usage:
Create: setcookie(name, value, expire, path, domain, secure)
Parameter explanation
参数 | 说明 |
name | 必须, 名称 |
value | 必须, cookie值 |
expire | 有效期 |
path | 服务器路径 |
domain | 域名 |
secure | 是否通过https传送 |
Read: $_COOKIE['xxx']Read
Delete:
Use setcookie(); setcookie("name" , "" , time()-1);
Let’s look at SESSION again
Overview:
To prevent users from logging in again on every page they open, use Session
Session is stored on the client, and verification is more efficient than cookies (there is no need to go to the Database to check the information every time)
Usage:
Start: session_start();
Usage: $_SESSION['name'] = "Matter";
Delete:
unset($_SESSION['name']);//Delete a single attribute
session_destroy();//Delete the entire session and clear all resources!
$_SESSION = array();//Delete all sessions
Excerpted from matter605924657