php学习笔记之cookie/session
今天学习了cookie和session,终于知道登录神马的是怎么弄的了,原谅我这个小菜鸟吧。没有比解开神秘面纱神马的最令人激动的了。下面来 回顾一下学习的内容: 1、cookie和session简介: 老听说cookie,以前也在IE上清理过这玩意,但是还真的没有认真的学习和
今天学习了cookie和session,终于知道登录神马的是怎么弄的了,原谅我这个小菜鸟吧。没有比解开神秘面纱神马的最令人激动的了。下面来回顾一下学习的内容:
1、cookie和session简介:
老听说cookie,以前也在IE上清理过这玩意,但是还真的没有认真的学习和了解过这个神器的小东西到底是什么。在非常多时候,我们需要跟踪浏览者在整个网站的活动,对他们身份进行自动或半自动的识别(也就是平时常说的网站登陆之类的功能),这时候,我们常采用Cookie与 Session来跟踪和判断。cookie和session就是登录界的两把神器。
那么这两把神器有什么区别呢?
Session信息是存放在server端,但session id是存放在client cookie的,当然php的session存放方法是多样化的,这样就算禁用cookie一样可以跟踪的。
Cookie是完全保持在客户端的如:IE firefox 当客户端禁止cookie时将不能再使用。
哦明白了,原来是是存放数据的位置不同,原来这两把神器放在两个不同的盒子里,一个是保存在浏览器里面的(cookie),一个是保存在服务器端的或者其他保存方式。
那么还有什么区别么呢?
这两把神器有不同的优缺点:
Cookie:是保持在客户端(浏览器等),因此就减少了服务器端的压力,同时能够长时间保存cookie信息。但是因为保存在客户端,因此安全性比较差,比如一些伪cookie等。可以通过加密来增加安全性。
Session:以为是保存在服务器端的,用户是无法修改的。因此安全性比较高,但是因为保存的数据比较大,会增大服务器的压力。
那么已经初步了解到了这两把神器,那么他们怎么使用呢?接着往下看。
2.cookie的配置应用:
(1)配置cookie的函数:
Setcookie(string name, string value, int expire,string path, string domain, int secure);
来看一下各个参数的意义,name就是cookie参数的名字,value就是值,expire是cookie保存的时间,path是保存的路径,domain 表示cookie变量的网站;secure 则需在 https 的安全传输时才有效。 其中name和value是必写的变量,其他变量可选。
下面举个例子:
SetCookie("Cookie", "value",time()+3600, "/www", ".baidu.com", 1);
time()+3600表示保存一个小时。1表示https。
(2)cookie的使用:全局函数$_COOKIE[]用法类似与$_POST[]和$_GET[];
(3)如何删除cookie:
有两种方法:一:set_cookie("name","");把cookie的值变为空。
二:set_cookie("name","value",time()+(-1)/time());把时间赋为0或者是-1。
下面是我写的一个具体例子:
<?php //删除cookie if ($_POST[out]) { setcookie("id",''); setcookie("pass",''); echo "退出成功"; echo "<script>location.href='cookie.php'"; } //设置cookie,判断是否输入了帐号密码。 if ($_POST[name]&&$_POST[password]) { setcookie("id",$_POST[name]); setcookie("pass",$_POST[password]); echo "<script>location.href='cookie.php'</script>";//注意,因为cookie不是实时生效的,因此第一次不能把cookie直接显示出来,因此要加一个跳转的js// } if ($_COOKIE[id]&&$_COOKIE[pass]) { echo "登录成功<br>帐号:".$_COOKIE[id]."<br>密码:".$_COOKIE[pass]; } else { echo "帐号或密码错误"; } ?>
3.Session的配置应用:
session_start(); //初始化session.需在文件头部
$_SESSION[name]=value; //配置Seeeion
echo $_SESSION[name]; //使用session
isset($_SESSION[name]); // 判断
unset($_SESSION[name]); //删除
session_destroy(); //消耗所有session
继续上代码:
<?php session_start(); if ($_POST[out]) { unset($_SESSION[id]); unset($_SESSION[pass]); # code... } if ($_POST[name]&&$_POST[password]) { $_SESSION[id]=$_POST[name]; $_SESSION[pass]=$_POST[password]; } if ($_SESSION[id]&&$_SESSION[pass]) { echo "登录成功<br>帐号:".$_SESSION[id]."<br>密码:".$_SESSION[pass]; } else { echo "帐号或密码错误"; } ?>
好了,cookie和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



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
