search
HomeBackend DevelopmentPHP TutorialA brief analysis of cookie and session technology in PHP_PHP Tutorial

1.What are cookies?

Cookie refers to the data (usually encrypted) stored on the user's local terminal by some websites in order to identify the user's identity and perform session tracking.

To put it simply, you go to a specialty store or supermarket to buy something, and then the store will apply for a membership card for you. In the future, your identity and purchase information will be stored in this card, and this card will store your On the body. After that, you only need to swipe the card every time you go to buy something, and there is no need to register or record other information.

Then map this paragraph to the web. The supermarket checkout counter is the server, and you yourself are the client. The card you carry is the cookie file stored in the client, which records you. account password and other information.

However, one thing to note is that the cookie will only take effect the second time it is used. That is to say, when you buy something for the first time in the supermarket, they will apply for a card for you, and you can swipe the card for future purchases. However, before the first purchase, the supermarket does not have any information about you, so you don’t have a card at all for the first time. The same goes for websites. When you log in to a website for the first time, of course you have to enter your account, password and other information, and then you can generate a cookie and store it locally for next time use.

At the same time, cookies also have their own validity period. After the expiration, they will become invalid and the local cookie files will be automatically deleted. You need to log in again, enter your account and password, and then generate a new cookie. The main purpose of doing this is for safety reasons.

2. Cookie mechanism diagram.

3. How to use cookies.

(1) Set cookie

bool setcookie ( string $name,$value,$expire,$path,$domain,$secure,$httponly  
For example:

setcookie("username","user",0,"/");
setcookie("username","user",time()+60*60,"/");

The usage of each parameter will not be explained. Here we focus on analyzing the time and path in the above two methods of setting cookies.

Put a 0 in the first time, does it mean that the survival time is 0. It is obviously impossible. It has a special meaning, indicating that the validity period of the cookie ends when the browser is closed. They all put a "/" in their paths. This means that all content paths under this domain name can access the cookie, which means that all pages under this website can track this cookie.

(2) Delete cookies

setcookie("username","",time()-3600,"/");
It is the same thing as setting a cookie, except that there is no cookie value, the time is earlier than the current time, and then it expires.

(3) View cookies

print_r($_COOKIE);
Everyone must know this guy, so just waste your eyes on him.

-------------------------------------------------- ----------------------------------------

-----------------------------I am the dividing line---------- --------------------------------

-------------------------------------------------- ----------------------------------

1.What is session?

Session refers to the time interval between an end user communicating with the interactive system, usually referring to the time elapsed from registering to enter the system to logging out of the system.

The working principle of session (excerpted from Baidu) (1) When a session is enabled for the first time, a unique identifier is stored in a local cookie. (2) First use the session_start() function, and PHP loads the stored session variables from the session warehouse. (3) When executing a PHP script, register the session variable by using the session_register() function. (4) When the PHP script execution ends, the session variables that have not been destroyed will be automatically saved in the local session library under a certain path. This path can be specified by session.save_path in the php.ini file. The next time you browse the web page Can be loaded and used.
In fact, in layman's terms, when you go to the supermarket to buy things, the membership card you apply for records your information. However, the membership card is not saved with you, but is stored in the supermarket's system as data. Once registered, you can directly use. You can use it directly when you need it. But once you leave the supermarket, that membership card loses its validity until your next purchase. At the same time, the only identification of this membership card is you, and no one else can use your membership card. It's easy to understand if you directly take the seat.
One big difference between session and cookie is that session is used directly after registration, that is, it can be used after the first purchase, while cookie information is stored in the membership card after the first purchase, and then starts to be used the second time.

2. Diagram of session mechanism.

3. How to use session.

(1) Set session

session_start();

$_SESSION['username']="user";

每一次在使用session之前都需要进行开启session,就当是通常进门都先需要开门一样。而在设置session时和对变量进行赋值没有多大的区别,其实$_SESSION本身就是一个变量。

(2)删除session

这个相对步骤就多了点,而不是cookie里面一句话搞定。

//开启session
session_start();

//注销session
session_unset();

//销毁session
session_destroy();

//同时销毁本地cookie中的sessionid
setcookie(session_name(),"",time()-3600,"/");
(3)查看session

print_r($_SESSION);

1.cookie与session优缺点。

cookie本身是存放在客户端中,仅占用几kb的内存大小。每次登录网站的时候都会带上本地的cookie进行验证,省去了麻烦的重复输入。但是安全性不是很高,毕竟是存放在本地的文件,虽然都是进行加密了的,一旦电脑数据被盗取,cookie就很有可能会被获取。

session存放在服务器中,占中内存虽小,但是用户基数够大的情况下,会对服务器造成很大的负荷。但是,数据放在服务器上,总归风险降低了许多。虽说没有不透风的墙,不过风也是可以很小很小的,这比喻。。。有同学可能疑问,session使用时,会有sessionid存在本地,一旦获取能否登录。答案当然是否定的,因为每次的id都是不一样的。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/900032.htmlTechArticle浅析PHP中cookie与session技术 1.cookie是什么? cookie指某些网站为了辨别用户身份、进行session跟踪而储存在用户本地终端上的数据(通常经过加...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
SOA中的软件架构设计及软硬件解耦方法论SOA中的软件架构设计及软硬件解耦方法论Apr 08, 2023 pm 11:21 PM

​对于下一代集中式电子电器架构而言,采用central+zonal 中央计算单元与区域控制器布局已经成为各主机厂或者tier1玩家的必争选项,关于中央计算单元的架构方式,有三种方式:分离SOC、硬件隔离、软件虚拟化。集中式中央计算单元将整合自动驾驶,智能座舱和车辆控制三大域的核心业务功能,标准化的区域控制器主要有三个职责:电力分配、数据服务、区域网关。因此,中央计算单元将会集成一个高吞吐量的以太网交换机。随着整车集成化的程度越来越高,越来越多ECU的功能将会慢慢的被吸收到区域控制器当中。而平台化

新视角图像生成:讨论基于NeRF的泛化方法新视角图像生成:讨论基于NeRF的泛化方法Apr 09, 2023 pm 05:31 PM

新视角图像生成(NVS)是计算机视觉的一个应用领域,在1998年SuperBowl的比赛,CMU的RI曾展示过给定多摄像头立体视觉(MVS)的NVS,当时这个技术曾转让给美国一家体育电视台,但最终没有商业化;英国BBC广播公司为此做过研发投入,但是没有真正产品化。在基于图像渲染(IBR)领域,NVS应用有一个分支,即基于深度图像的渲染(DBIR)。另外,在2010年曾很火的3D TV,也是需要从单目视频中得到双目立体,但是由于技术的不成熟,最终没有流行起来。当时基于机器学习的方法已经开始研究,比

如何让自动驾驶汽车“认得路”如何让自动驾驶汽车“认得路”Apr 09, 2023 pm 01:41 PM

与人类行走一样,自动驾驶汽车想要完成出行过程也需要有独立思考,可以对交通环境进行判断、决策的能力。随着高级辅助驾驶系统技术的提升,驾驶员驾驶汽车的安全性不断提高,驾驶员参与驾驶决策的程度也逐渐降低,自动驾驶离我们越来越近。自动驾驶汽车又称为无人驾驶车,其本质就是高智能机器人,可以仅需要驾驶员辅助或完全不需要驾驶员操作即可完成出行行为的高智能机器人。自动驾驶主要通过感知层、决策层及执行层来实现,作为自动化载具,自动驾驶汽车可以通过加装的雷达(毫米波雷达、激光雷达)、车载摄像头、全球导航卫星系统(G

多无人机协同3D打印盖房子,研究登上Nature封面多无人机协同3D打印盖房子,研究登上Nature封面Apr 09, 2023 am 11:51 AM

我们经常可以看到蜜蜂、蚂蚁等各种动物忙碌地筑巢。经过自然选择,它们的工作效率高到叹为观止这些动物的分工合作能力已经「传给」了无人机,来自英国帝国理工学院的一项研究向我们展示了未来的方向,就像这样:无人机 3D 打灰:本周三,这一研究成果登上了《自然》封面。论文地址:https://www.nature.com/articles/s41586-022-04988-4为了展示无人机的能力,研究人员使用泡沫和一种特殊的轻质水泥材料,建造了高度从 0.18 米到 2.05 米不等的结构。与预想的原始蓝图

超逼真渲染!虚幻引擎技术大牛解读全局光照系统Lumen超逼真渲染!虚幻引擎技术大牛解读全局光照系统LumenApr 08, 2023 pm 10:21 PM

实时全局光照(Real-time GI)一直是计算机图形学的圣杯。多年来,业界也提出多种方法来解决这个问题。常用的方法包通过利用某些假设来约束问题域,比如静态几何,粗糙的场景表示或者追踪粗糙探针,以及在两者之间插值照明。在虚幻引擎中,全局光照和反射系统Lumen这一技术便是由Krzysztof Narkowicz和Daniel Wright一起创立的。目标是构建一个与前人不同的方案,能够实现统一照明,以及类似烘烤一样的照明质量。近期,在SIGGRAPH 2022上,Krzysztof Narko

internet的基本结构与技术起源于什么internet的基本结构与技术起源于什么Dec 15, 2020 pm 04:48 PM

internet的基本结构与技术起源于ARPANET。ARPANET是计算机网络技术发展中的一个里程碑,它的研究成果对促进网络技术的发展起到了重要的作用,并未internet的形成奠定了基础。arpanet(阿帕网)为美国国防部高级研究计划署开发的世界上第一个运营的封包交换网络,它是全球互联网的始祖。

一文聊聊智能驾驶系统与软件升级的关联设计方案一文聊聊智能驾驶系统与软件升级的关联设计方案Apr 11, 2023 pm 07:49 PM

由于智能汽车集中化趋势,导致在网络连接上已经由传统的低带宽Can网络升级转换到高带宽以太网网络为主的升级过程。为了提升车辆升级能力,基于为车主提供持续且优质的体验和服务,需要在现有系统基础(由原始只对车机上传统的 ECU 进行升级,转换到实现以太网增量升级的过程)之上开发一套可兼容现有 OTA 系统的全新 OTA 服务系统,实现对整车软件、固件、服务的 OTA 升级能力,从而最终提升用户的使用体验和服务体验。软件升级触及的两大领域-FOTA/SOTA整车软件升级是通过OTA技术,是对车载娱乐、导

综述:自动驾驶的协同感知技术综述:自动驾驶的协同感知技术Apr 08, 2023 pm 03:01 PM

arXiv综述论文“Collaborative Perception for Autonomous Driving: Current Status and Future Trend“,2022年8月23日,上海交大。感知是自主驾驶系统的关键模块之一,然而单车的有限能力造成感知性能提高的瓶颈。为了突破单个感知的限制,提出协同感知,使车辆能够共享信息,感知视线之外和视野以外的环境。本文回顾了很有前途的协同感知技术相关工作,包括基本概念、协同模式以及关键要素和应用。最后,讨论该研究领域的开放挑战和问题

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools