search
Homephp教程php手册浅谈PHP语法6

浅谈PHP语法6

Jun 13, 2016 am 10:27 AM
cookiephpsessionandat lastofgrammar

上文(《谈谈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”
?br> 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

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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor