search

正文:
 上文(《谈谈php语法(3)》的最后提到了Cookie和session,本文就这两种技术作一些介绍。
 Cookie我们都常把它念成“库记”,也叫它“小甜点”。它是一种存储在客户浏览器中的一个小文件。它是为解决HTTP的一次连接而无崐记忆而发展起来的,可用来追踪使用者或是对重返的使用者进行确认。PHP对此提供了setcookie()函数,可以设定Cookie。因为Cookies算是崐HTTP标头的一部分。所以,setcookie()函数必须在网页数据传给浏览器之前调用。这同调用header()函数是一样的。
 Cookie必须由主机端提供;所以,我们必须在CGI程序中送出一个设定了cookie的标头。如下是PHP中调用setcookie()函数设定cookie的崐例子:
setcookie("user","wind",time()+3600,"/php/","http://www.oso.com.cn");
?>
 其中,user为该cookie的名称;wind为该cookie的值;time()+3600为该cookie的有效时间;/php/为该cookie的相关路径;http://www.崐oso.com.cn为该cookie的网站。
 其实,我们除了使用这种方式设定cookie外,还可使用header()函数如:header("Set-Cookie:user=wind"),不过这要对HTTP的头部信崐息有所了解才行,所以笔者不建议使用这种方式,还是使用setcookie()方便些。
 读取cookie时,浏览器在连接某个网站时,会自动先检查是否有该站的cookie,有的话将会自动传给服务器,而在PHP中,会将传回的co崐okie作为一个变量。如上面所设的cookie返回后,会形成一个$user变量,其值为wind。
 但是,cookie有个致命的缺点,就是若客户关了cookie接收,无法向客户端存放cookie时,一切操作就会出错。因此,在PHP4中提供了S崐ession来代替Cookie。
 Session与Cookie的最大区别在于Cookie将信息存于客户端,而Session则是存于服务器端。其实,Session是对PHP脚本提供了一个全局崐变量。例子如下:
 设置一个Session,名为user,值为wind
session_start();
$user="wind";
session_register("user");
?>
 读Session,显示结果为“欢迎你!wind”
session_start();
echo "欢迎你!$user";
?>
 本文只是浅谈了一下Cookie和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 Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

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

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

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor