一、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。
以上所述就是本文的全部内容了,希望大家能够喜欢。

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.


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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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

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
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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool