浅谈COOKIE和SESSION区别,浅谈cookiesession
浅谈COOKIE和SESSION区别,浅谈cookiesession
一、cookie介绍
cookie 常用于识别用户。cookie 是服务器留在用户计算机中的小文件。每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie。通过 PHP,您能够创建并取回 cookie 的值。
1、设置Cookie
PHP用SetCookie函数来设置Cookie。
SetCookie函数定义了一个Cookie,并且把它附加在HTTP头的后面,SetCookie函数的原型如下:
int SetCookie(string name, string value, int expire, string path, string domain, int secure);
参数说明:cookie名称,cookie值,过期时间(int),有效路径,有限域名,https传递才有效
注意:当前设置的Cookie不是立即生效的,而是要等到下一个页面时才能看到.这是由于在设置的这个页面里Cookie由服务器传递给客户浏览器,在下一个页面浏览器才能把Cookie从客户的机器里取出传回服务器的原因。
使用例子:
普通使用:
setcookie('name','PHP淮北');
带失效时间的:
setcookie('name','PHP淮北',time()+24*60*60);//1day
Cookie是面向路径的 ,默认存储在当前文件下,如果没有设置路径,不同文件下的cookie默认保存在不同文件夹下,如图:默认保存在mytest文件夹下
2、接收和处理Cookie
用户端与服务端的web通信协议是http。而PHP通过http取得用户数据惯用的三种方法分别是:POST方法、GET方法还有Cookie。而PHP默认传递方法正是Cookie,也是最佳方法。
比如设置一个名为MyCookier的Cookie,PHP会自动从WEB服务器接收的HTTP头里把它分析出来,并形成一个与普通变量一样的变量,名为$myCookie,这个变量的值就是Cookie的值
3,删除Cookie
要删除一个已经存在的Cookie,有两个办法:
一是调用只带有name参数的SetCookie,那么名为这个name的Cookie将被从关系户机上删掉;例如:setcookie('name','');
另一个办法是设置Cookie的失效时间为time()或time()-1,那么这个Cookie在这个页面的浏览完之后就被删除了(其实是失效了)。 例如:setcookie('name','PHP淮北',time()-24*60*60);
要注意的是,当一个Cookie被删除时,它的值在当前页在仍然有效的。
使用Cookie的注意事项:
首先是必须在HTML文件的内容输出之前设置(Cookie是HTTP协议头的一部分,用于浏览器和服务器之间传递信息,所以必须在任何属于HTML文件本身的内容输出之前调用Cookie函数。
在PHP页面可以先使用
ob_start();//开启
code…..
ob_end_flush(); //刷新缓存
可以防止header提示错误);
不同的浏览器对Cookie的处理机制不一样
cookie限制是在客户端的。一个浏览器能创建的Cookie数量最多为30个,并且每个不能超过4KB,每个WEB站点能设置的Cookie总数不能超过20个。
当前设置的Cookie不是立即生效的,而是要等到下一个页面时才能看到
二、session介绍
session机制是一种服务器端的机制,服务器使用一种类似于散列表的结构(也可能就是使用散列表)来保存信息,每一个网站访客都会被分配给一个唯一的标志符,即会话ID,它的存放形式无非两种:要么经过url传递,要么保存在客户端的Cookies里.当然,你也可以将Session保存到数据库里,这样会更安全,但效率方面会有所下降.url方式传递安全性肯定太差,PHP的会话机制是通过设置Cookie,在Cookie中保存会话id(Session ID),在服务器端会生成session文件,与用户进行关联,Web应用程序存储与这些Session相关的数据,并在各页面间进行传递.
PHP相关函数
在PHP中有关Session的函数比较多,不过我们最常用到的也就这么几个函数:
session_start():启用session机制,在需要用到session的程序文件的最开始调用它.
session_register():注册session变量
session_unregister(): 删除session变量(一个一个删除)
session_is_registered(): 判断session变量是否注册
session_distroy(): 销毁所有session变量(所有session变量销毁,包括文件)
需要注意下面几个方面:
1.函数session_start()必须在程序最开始执行,在其前面不能有任何输出内容,否则
就会出现“Warning:Cannot send session cookie - headers already
sent"类似这样的警告信息.
2.函数session_register()用于注册要保存在session中的相关变量,其用法如下:
<?php $val = "session value"; session_register("val"); ?>
val即为要注册的session变量名,在注册时一定不要加上"$"符号,只写其变量名称即可.
3.函数session_unregister()与上面函数用法完全相同,但功能相反,上面函数是注册
session变量,而其则是删除指定的session变量.
4.函数session_is_registered()用于判断session变量是否注册.
5.函数session_destroy()主要用于在系统注销和退出时,销毁所有的session变量,它没有参数,直接调用即可。
Session与PHP.ini的关系配置
1,session.save_handler = file
用于读取/回写session数据的方式,默认是files。它会让PHP的session管理函数使用指定的文本文件存储session数据
2,session.save_path = “/xammp/temp/”
指定保存session文件的目录,可以指定到别的目录,但是指定目录必须要有httpd守护进程属主(比如apache或www等)写权限,否则无法回存session数据。它还可以写成这样session.save_path = “N;/path” 其中N是整数。这样使得不是所有的session文件都保存在同一个目录中,而是分散在不同目录。这对于服务器处理大量session文件是很有帮助的。(注:目录需要自己手工创建)
3,session.auto_start = 0
如果启用该选项,用户的每次请求都会初始化session。不推荐使用,最好通过session_start()显示地初始化session。
以上所述就是本文的全部内容了,希望大家能够喜欢。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

There is no built-in sum function in C language, so it needs to be written by yourself. Sum can be achieved by traversing the array and accumulating elements: Loop version: Sum is calculated using for loop and array length. Pointer version: Use pointers to point to array elements, and efficient summing is achieved through self-increment pointers. Dynamically allocate array version: Dynamically allocate arrays and manage memory yourself, ensuring that allocated memory is freed to prevent memory leaks.

The difference between Ethereum and Bitcoin is significant. Technically, Bitcoin uses PoW, and Ether has shifted from PoW to PoS. Trading speed is slow for Bitcoin and Ethereum is fast. In application scenarios, Bitcoin focuses on payment storage, while Ether supports smart contracts and DApps. In terms of issuance, the total amount of Bitcoin is 21 million, and there is no fixed total amount of Ether coins. Each security challenge is available. In terms of market value, Bitcoin ranks first, and the price fluctuations of both are large, but due to different characteristics, the price trend of Ethereum is unique.

In C language, the main difference between char and wchar_t is character encoding: char uses ASCII or extends ASCII, wchar_t uses Unicode; char takes up 1-2 bytes, wchar_t takes up 2-4 bytes; char is suitable for English text, wchar_t is suitable for multilingual text; char is widely supported, wchar_t depends on whether the compiler and operating system support Unicode; char is limited in character range, wchar_t has a larger character range, and special functions are used for arithmetic operations.

The core difference between bean bun and DeepSeek is retrieval accuracy and complexity. 1. Doubao is based on keyword matching, simple and direct, with low cost, but low accuracy, and is only suitable for structured data; 2. DeepSeek is based on deep learning, can understand semantics, has high accuracy, but high cost, and is suitable for unstructured data. The final choice depends on the application scenario and resource limitations. If the accuracy requirements are not high, choose bean bags, and if you pursue high precision, choose DeepSeek.

C language functions are the basis for code modularization and program building. They consist of declarations (function headers) and definitions (function bodies). C language uses values to pass parameters by default, but external variables can also be modified using address pass. Functions can have or have no return value, and the return value type must be consistent with the declaration. Function naming should be clear and easy to understand, using camel or underscore nomenclature. Follow the single responsibility principle and keep the function simplicity to improve maintainability and readability.

The collaborative working mechanism between Apache or Nginx and PHP: Comparison of mod_php5, php-cgi and php-fpm is to use Apache or Nginx to build a web server and use PHP for backend...

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...
