session和cookie区别

<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>

1.session
(1)session存储在服务器的
(2)session每个人存一份
(3)session有默认的过期时间
(4)session里面可以存储任意类型的数据
安全,对服务造成压力
用法:
1.当一个页面需要使用session的时候,需要在页面顶部加session_start();
2.操作session
    赋值  $_SEESION["uid"] = "";
    取值  $_SESSINO["uid"]

2.cookie
(1)cookie存储在客户端的
(2)cookie每人存一份
(3)cookie没有默认过期时间
(4)cookie只能存储字符串
不安全,不会对服务器造成压力
用法:
1.操作cookie
    赋值:setcookie(key,value)
    取值:$_COOKIE["uid"]

<?php
    $_SESSION["uid"] = "zhangsan";
    
    setcookie("uid","lisi");
?>


</body>
</html>

2.除了登录页面,每个页面都要加上验证如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<?php
session_start();

if(empty($_SESSION["uid"]))
{
    header("location:login.php");
    exit;
}

?>


</body>
</html>
原文地址:https://www.cnblogs.com/zxl89/p/6048284.html