Home Backend Development PHP Tutorial PHP对话控制SESSION与COOKIE介绍

PHP对话控制SESSION与COOKIE介绍

Jun 13, 2016 pm 12:21 PM
cookie session setcookie start

PHP会话控制SESSION与COOKIE介绍

会话控制产生的背景介绍
实现用户跟踪的几个方法:
1、当前页面的变量 page
在当前页面可用,在页面执行完成之后,变量释放了。
2、两个页面之间传递变量 get
通过URL进行传值/abc.php?id=12234&age=2 能够做用户跟踪但是太繁琐。
3、会话级别 session
同一个用户在同一个网站共享自己的变量。
4、全局的 glboal
文件、数据库。任何人、任何网站我都能够使用它们。总之,会话控制其实就是许服务器跟踪同一个客户端做出的连续的请求。
SESSION和COOKIE的区别
cookie:最主要的是服务器端给客户端的信息让客户端存储。原理:一个用户在访问我服务器的时,服务器脚本语言根据业务逻辑设置cookie给用户(登录),用户在客户端电脑特定的文件夹下创建这个网站的文本(通常和域名有关系),该文本主要用于记录要保存的变量,访问多个网站那就创建多个这个文件,用户在访问其他页面的时候http协议会通过httpd的头将客户端设置的文本里面的内容带给其他页面以供使用。多个人访问就是上一步骤的重复执行。
这个保存网站信息的文本文件叫cookie文件,一整套这样的机制叫cookie机制。
Firefox(火狐浏览器)中,cookie信息不是普通的文本信息,加密过了。可以在浏览器中直接查看:
工具->选项->隐私->历史记录->使用自定义历史记录设置->显示cookie
其实在早期cookie的使用是很有争议的,在指定的文件夹下,写指定的文件而且这个文件肯定是个文本。
设置方法:
1、设置cookie setcookie(名,值,保存时间);
注意:
⑴如果保存的时间不写,代表的就是关闭浏览器的时候cookie就消失了。
⑵取cookie使用COOKIE访cookiecookie2setcookie(,,)time()?1cookie使setCookie使_COOKIE[”]=xxoo这种方式设置;
⑵setcookie设置值的时候第二个参数不能是array类型的;
⑶setcookie里面可以存数组,但是要用xxoo[xx]这种方式来存数组。数组可以是索引的也可以是关联的;
⑷如果setcookie里面使用索引数组但是你不给下标的这种格式,那么下标永远是0,后面的值覆盖前面的值;
⑸当你设置完cookie之后如果你在同一个页面马上使用cookie值第一次是不能用的。因为在你请求的同时服务器端刚刚让客户端存;
⑹setcookie里面有第四个参数,就是path就是cookie设置的路径,默认cookie的路径是当前目录下。
3、session:
cookie的升级版,cookie会占用空间、好过电脑账号泄露是通过读取网站COOKIE实现的。
原理:客户端第一次登录网站的时候根据业务逻辑会分配给客户端一个不重复的ID,这个时候服务器端会建立一个文件,这个文件的命名是以分配用户的sessionid 命名的,当客户端拿到sessionid后会以cookie的形式进行存储,如果下次再访问页面将会以cookie的形式将sessionid带过来。带过来的时候,服务器会根据session的机制去读取session id的文件。
4、如何设置session
session_start();开启session的机制 开启session
机制:
⑴当客户端第一次请求服务器的时session_start会分配ID创建session文件。
⑵如果你已经设置过session,它会根据HTTP协议带过来的session id找服务器里面的文件。
5、如何拿值、设置值
SESSION[xxoo]=xxoo;_SESSION[‘xxoo’] 取值也要先开启session start
6、如何销毁session
⑴session_start();
⑵清空$_SESSION数组 unset array(); //因为第三部里面虽然文件没了但是里面的值还在内存里面呢,还能用。
⑶销毁session文件 session_destory();
7、删除客户端session ID
setcookie(‘session ID值’,”,time()-1);
setcookie(session_name(),”,time()-1);
注意:
删除session id、setcookie的时,最好把第四个参数加上
session_name();来动态获得sessionID的值。
php.ini中session.name选项代表的session的名
session.auto_start这个代表的是不用session_start而进入每个页面时自动开启session通常都会关掉。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

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.

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.

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.

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

Where are the mobile cookies? Where are the mobile cookies? Dec 22, 2023 pm 03:40 PM

Cookies on the mobile phone are stored in the browser application of the mobile device: 1. On iOS devices, Cookies are stored in Settings -> Safari -> Advanced -> Website Data of the Safari browser; 2. On Android devices, Cookies Stored in Settings -> Site settings -> Cookies of Chrome browser, etc.

What are the dangers of cookie leakage? What are the dangers of cookie leakage? Sep 20, 2023 pm 05:53 PM

The dangers of cookie leakage include theft of personal identity information, tracking of personal online behavior, and account theft. Detailed introduction: 1. Personal identity information is stolen, such as name, email address, phone number, etc. This information may be used by criminals to carry out identity theft, fraud and other illegal activities; 2. Personal online behavior is tracked and analyzed through cookies With the data in the account, criminals can learn about the user's browsing history, shopping preferences, hobbies, etc.; 3. The account is stolen, bypassing login verification, directly accessing the user's account, etc.

Detailed explanation of where browser cookies are stored Detailed explanation of where browser cookies are stored Jan 19, 2024 am 09:15 AM

With the popularity of the Internet, we use browsers to surf the Internet have become a way of life. In the daily use of browsers, we often encounter situations where we need to enter account passwords, such as online shopping, social networking, emails, etc. This information needs to be recorded by the browser so that it does not need to be entered again the next time you visit. This is when cookies come in handy. What are cookies? Cookie refers to a small data file sent by the server to the user's browser and stored locally. It contains user behavior of some websites.

How cookies work How cookies work Sep 20, 2023 pm 05:57 PM

The working principle of cookies involves the server sending cookies, the browser storing cookies, and the browser processing and storing cookies. Detailed introduction: 1. The server sends a cookie, and the server sends an HTTP response header containing the cookie to the browser. This cookie contains some information, such as the user's identity authentication, preferences, or shopping cart contents. After the browser receives this cookie, it will be stored on the user's computer; 2. The browser stores cookies, etc.

See all articles