PHP5中Cookie与 Session使用详解
1、Cookie和Session简介与区别
在非常多时候,我们需要跟踪浏览者在整个网站的活动,对他们身份进行自动或半自动的识别(也就是平时常说的网站登陆之类的功能),这时候,我们常采用Cookie与 Session来跟踪和判断。
区别
Session信息是存放在server端,但session id是存放在client cookie的,当然php的session存放方法是多样化的,这样就算禁用cookie一样可以跟踪
Cookie是完全保持在客户端的如:IE firefox 当客户端禁止cookie时将不能再使用
2、Cookie的配置与应用
Setcookie(string name, string value, int expire,string path, string domain, int secure);
其中name是cookie变量名称标识,你在php中将能象使用普通变量名相同来用他引用cookie变量。value是cookie变量的初始值,expire 表示该cookie变量的有效时间;path 为该cookie变量的相关路径;domain 表示cookie变量的网站;secure 则需在 https 的安全传输时才有效。
SetCookie("Cookie", "cookievalue",time()+3600, "/forum", ".php100.com", 1);
接收和处理Cookie
PHP对Cookie的接收和处理的支持非常好,是完全自动的,跟FORM变量的原则一样,特别简单。
比如设置一个名为 MyCookier的Cookie,PHP会自动从WEB服务器接收的HTTP头里把它分析出来,并形成一个与普通变量一样的变量,名为$ myCookie,这个变量的值就是Cookie的值。数组同样适用。另外一个办法是引用PHP的全局变量$HTTP_COOKIE_VARS数组。
分别举例如下:(假设这些都在以前的页面里设置过了,并且仍然有效)
echo $MyCookie;
echo $CookieArray[0];
echo $_COOKIE["MyCookie"];
echo $HTTP_COOKIE_VARS["MyCookie"];
删除Cookie
要删除一个已经存在的Cookie,有两个办法:
1、SetCookie("Cookie", "");
2、SetCookie("Cookie", "value" , time()-1 / time() );
使用Cookie的限制
1、必须在HTML文件的内容输出之前设置;
2、不同的浏览器对Cookie的处理不一致,且有时会出现错误的结果。
3、限制是在客户端的。一个浏览器能创建的Cookie数量最多为30个,并且每个不能超过4KB,每个WEB站点能设置的Cookie总数不能超过20个。
3、Session的配置与应用
复制代码 代码如下:
session_start(); //初始化session.需在文件头部
$_SESSION[name]=value; //配置Seeeion
echo $_SESSION[name]; //使用session
isset($_SESSION[name]); // 判断
unset($_SESSION[name]); //删除
session_destroy(); //消耗所有session
注意:session_register(),session_unregister,session_is_registered在php5下不再使用
//cookies用法实例
复制代码 代码如下:
if($_GET['out'])
{ //用于注销cookies
setcookie('id',"");
setcookie('pass',"");
echo "<script>location.href='login.php'</script>"; //因为cookies不是及时生效的,只有你再次刷新时才生效,所以,注销后让页面自动刷新。
}
if($_POST['name']&&$_POST['password']) //如果变量用户名和密码存在时,在下面设置cookies
{ //用于设置cookies
setcookie('id',$_POST['name'],time()+3600);
setcookie('pass',$_POST['password'],time()+3600);
echo "<script>location.href='login.php'</script>"; //让cookies及时生效
}
if($_COOKIE['id']&&$_COOKIE['pass'])
{ //cookies设置成功后,用于显示cookies
echo "登录成功!
用户名:".$_COOKIE['id']."
密码:".$_COOKIE['pass'];
echo "
";
echo "注销cookies"; //双引号内,如果再有引号,需要用单引号。
}
?>
//session用法实例
复制代码 代码如下:
//session用法实例
session_start();//启动session,必须放在第一句,否则会出错。
if($_GET['out'])
{
unset($_SESSION['id']);
unset($_SESSION['pass']);
}
if($_POST['name']&&$_POST['password'])
{
//用于设置session
$_SESSION['id']=$_POST['name'];
$_SESSION['pass']=$_POST['password'];
}
if($_SESSION['id']&&$_SESSION['pass'])
{
echo "登录成功!
用户ID:".$_SESSION['id']."
用户密码:".$_SESSION['pass'];
echo "
";
echo "注销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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











Export password-protected PDF in Photoshop: Open the image file. Click "File"> "Export"> "Export as PDF". Set the "Security" option and enter the same password twice. Click "Export" to generate a PDF file.

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

The necessity of registering VueRouter in the index.js file under the router folder When developing Vue applications, you often encounter problems with routing configuration. Special...

The key differences between CentOS and Ubuntu are: origin (CentOS originates from Red Hat, for enterprises; Ubuntu originates from Debian, for individuals), package management (CentOS uses yum, focusing on stability; Ubuntu uses apt, for high update frequency), support cycle (CentOS provides 10 years of support, Ubuntu provides 5 years of LTS support), community support (CentOS focuses on stability, Ubuntu provides a wide range of tutorials and documents), uses (CentOS is biased towards servers, Ubuntu is suitable for servers and desktops), other differences include installation simplicity (CentOS is thin)

Detailed explanation of XPath search method under DOM nodes In JavaScript, we often need to find specific nodes from the DOM tree based on XPath expressions. If you need to...

不同数据库系统添加列的语法为:MySQL:ALTER TABLE table_name ADD column_name data_type;PostgreSQL:ALTER TABLE table_name ADD COLUMN column_name data_type;Oracle:ALTER TABLE table_name ADD (column_name data_type);SQL Server:ALTER TABLE table_name ADD column_name data_

There are differences in the promotion methods of H5 and mini programs: platform dependence: H5 depends on the browser, and mini programs rely on specific platforms (such as WeChat). User experience: The H5 experience is poor, and the mini program provides a smooth experience similar to native applications. Communication method: H5 is spread through links, and mini programs are shared or searched through the platform. H5 promotion methods: social sharing, email marketing, QR code, SEO, paid advertising. Mini program promotion methods: platform promotion, social sharing, offline promotion, ASO, cooperation with other platforms.

Laravel and ThinkPHP are both popular PHP frameworks and have their own advantages and disadvantages in development. This article will compare the two in depth, highlighting their architecture, features, and performance differences to help developers make informed choices based on their specific project needs.
