会话控制是一种面向连接的可靠通信方式,通常根据会话控制记录判断用户登录的行为。比如:网购和处理邮件时,你可能需要访问多个页面,但只要你不退出,在同一个系统上,多个页面之间互相切换时,还能保持用户的登录信息。
但是http是无状态的协议,不能维护两个事物之间的状态。但一个用户在请求一个页面以后再请求另外一个页面时,还要让服务器知道这是同一个用户。可以使用cookie和session解决这个问题。
cookies(小甜饼的意思)是一种由服务器发送给客户端的片段信息,存储在客户端浏览器的内存或硬盘上,在客户对该服务的请求中发回它。
格式和描述:
bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, int secure]]]]])
setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including and tags as well as any whitespace. If output exists prior to calling this function, setcookie() will fail and return FALSE. If setcookie() successfully runs, it will return TRUE. This does not indicate whether the user accepted the cookie.
setcookie()函数的参数说明参数描述示例$namecookie的识别名称 $valuecookie的值 $expirecookie的生存期限 $pathcookie在服务器端生效的路径 $domaincookie所属服务器域名 $secure设置cookie是否仅通过https连接 一个简单的登录系统: