search
HomeBackend DevelopmentPHP TutorialPHP对话控制SESSION与COOKIE介绍

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
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
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

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 Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.