php5 的 session 详解其一: 什么是 session?
1、什么是Session Session 的中文译名叫做“会话”,其本来的含义是指 有始有终的一系列动作/消息 ,比如打电话时从拿起电话拨号到挂断电话这中间的一系列过程可以称之为一个 session。目前社会上对 session 的理解非常混乱:有时候我们可以看到这样的话“在一
1、什么是Session
Session 的中文译名叫做“会话”,其本来的含义是指有始有终的一系列动作/消息,比如打电话时从拿起电话拨号到挂断电话这中间的一系列过程可以称之为一个 session。目前社会上对 session 的理解非常混乱:有时候我们可以看到这样的话“在一个浏览器会话期间,...”,这里的会话是指从一个浏览器窗口打开到关闭这个期间;也可以看到“用户(客户端)在一次会话期间”这样一句话,它可能指用户的一系列动作(一般情况下是同某个具体目的相关的一系列动作,比如从登录到选购商品到结账登出这样一个网上购物的过程;然而有时候也可能仅仅是指一次连接;其中的差别只能靠上下文来推断了。然而当 session 一词与网络协议相关联时,它又往往隐含了“面向连接”和/或“保持状态”这样两个含义,“面向连接”指的是在通信双方在通信之前要先建立一个通信的渠道,比如打电话,直到对方接了电话通信才能开始 。“保持状态”则是指通信的一方能够把一系列的消息关联起来,使得消息之间可以互相依赖,比如一个服务员能够认出再次光临的老顾客并且记得上次这个顾客还欠店里一块钱。这一类的例子有“一个 TCP session”或者“一个 POP3 session”。
鉴于这种混乱已不可改变,要为 session 下个定义就很难有统一的标准。而在阅读 session 相关资料时,我们也只有靠上下文来推断理解了。不过我们可以这样理解:例如我们打电话,从拨通的那一刻起到挂断电话期间,因为电话一直保持着接通的状态,所以把这种接通的状态叫做 session。它是访客与整个网站交互过程中一直存在的公有变量,在客户端不支持 COOKIE 的时候,为了保证数据正确、安全,就采用 SESSION 变量。访问网站的来客会被分配一个唯一的标识符,即所谓的会话 ID。它要么存放在客户端的 cookie,要么经由 URL 传递。SESSION 的发明填补了 HTTP 协议的局限:HTTP 协议被认为是无状态协议,无法得知用户的浏览状态,当它在服务端完成响应之后,服务器就失去了与该浏览器的联系。这与 HTTP 协议本来的目的是相符的,客户端只需要简单的向服务器请求下载某些文件,无论是客户端还是服务器都没有必要纪录彼此过去的行为,每一次请求之间都是独立的,好比一个顾客和一个自动售货机或者一个普通的(非会员制)大卖场之间的关系一样。
物车一直扮演着临时存贮被选商品的角色,用它追踪用户在网站上的活动情况,这就是 SESSION 的作用,它可以用于用户身份认证,程序状态记录,页面之间参数传递等。SESSION 的实现中采用 COOKIE 技术,SESSION 会在客户端保存一个包含session_id(SESSION 编号)的 COOKIE;在服务器端保存其他 session 变量,比如 session_name 等等。当用户请求服务器时也把 session_id 一起发送到服务器,通过 session_id 提取所保存在服务器端的变量,就能识别用户是谁了。同时也不难理解为什么 SESSION 有时会失效了。
当客户端禁用 COOKIE 时(点击 IE 中的“工具”—“internet="">Internet选项”,在弹出的对话框里点击“安全”—“自定义级别”项,将“允许每个对话 COOKIE”设为禁用),session_id 将无法传递,此时 SESSION 失效。不过 php5在 linux/unix 平台可以自动检查 cookie 状态,如果客户端设置了禁用,则系统自动把 session_id 附加到 url 上传递。windows 主机则无此功能。
2、Session 常见函数及用法?● Session_start() :开始一个会话或者返回已经存在的会话。说明:这个函数没有参数,且返回值均为 true。如果你使用基于 cookie 的session(cookie-based sessions),那么在使用 Session_start()之前浏览器不能有任何输出,否则会发生以下错误:
Warning: Cannot send session cache limiter - headers already sent (outputstarted at /usr/local/apache/htdocs/cga/member/1.php:2)............
你可以在 php="">php.ini 里启动 session.auto_start=1,这样就无需每次使用session之前都要调用session_start()。但启用该选项也有一些限制,如果确实启用了session.auto_start,则不能将对象放入会话中,因为类定义必须在启动会话之前加载以在会话中重建对象。请求结束后所有注册的变量都会被序列化。已注册但未定义的变量被标记为未定义。在之后的访问中这些变量也未被会话模块定义,除非用户以后定义它们。警 告 : 有 些 类 型 的数 据 不 能 被 序 列化 因 此 也 就 不 能 保存 在 会 话 中 。 包括resource 变量或者有循环引用的对象(即某对象将一个指向自己的引用传递给另一个对象)。
●注册 SESSION 变量:
PHP5 使用$_SESSION[‘xxx’]=xxx 注册 SESSION 全局变量。和 GET,POST,COOKIE的使用方法相似。
注意:
session_register(),
session_unregister(),
session_is_registered()
在 php5 下不再使用,除非在 php.ini里把register_globle设为on,不过出于安全考虑,强烈建议关闭register_globle。HTTP_SESSION_VARS也不提倡使用了,官方建议用$_SESSION 代替之。例如:
//Page2.php <?php session_start(); echo $_SESSION['name']; // echo $_SESSION['passwd']; // echo date('Y m d H:i:s', $_SESSION['time']); echo '<br /><a href="page1.php">返回山一页</a>'; ?>

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



How to set up keyboard startup on Gigabyte's motherboard. First, if it needs to support keyboard startup, it must be a PS2 keyboard! ! The setting steps are as follows: Step 1: Press Del or F2 to enter the BIOS after booting, and go to the Advanced (Advanced) mode of the BIOS. Ordinary motherboards enter the EZ (Easy) mode of the motherboard by default. You need to press F7 to switch to the Advanced mode. ROG series motherboards enter the BIOS by default. Advanced mode (we use Simplified Chinese to demonstrate) Step 2: Select to - [Advanced] - [Advanced Power Management (APM)] Step 3: Find the option [Wake up by PS2 keyboard] Step 4: This option The default is Disabled. After pulling down, you can see three different setting options, namely press [space bar] to turn on the computer, press group

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

Which tablet is suitable for musicians? The 12.9-inch speaker in Huawei’s iPad is a very good product. It comes with four speakers and the sound is excellent. Moreover, it belongs to the pro series, which is slightly better than other styles. Overall, ipad pro is a very good product. The speaker of this mini4 mobile phone is small and the effect is average. It cannot be used to play music externally, you still need to rely on headphones to enjoy music. Headphones with good sound quality will have a slightly better effect, but cheap headphones worth thirty or forty yuan cannot meet the requirements. What tablet should I use for electronic piano music? If you want to buy an iPad larger than 10 inches, I recommend using two applications, namely Henle and Piascore. Provided by Henle

What games can be played with i34150 with 1G independent graphics? Can it play small games such as LoL? GTX750 and GTX750TI are very suitable graphics card choices. If you just play some small games or not play games, it is recommended to use the i34150 integrated graphics card. Generally speaking, the price difference between graphics cards and processors is not very big, so it is important to choose a reasonable combination. If you need 2G of video memory, it is recommended to choose GTX750TI; if you only need 1G of video memory, just choose GTX750. GTX750TI can be seen as an enhanced version of GTX750, with overclocking capabilities. Which graphics card can be paired with i34150 depends on your needs. If you plan to play stand-alone games, it is recommended that you consider changing the graphics card. you can choose

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions

In the process of using the Windows 10 operating system developed by Microsoft, many users are curious and confused about the new technology called Cortana. Cortana's official name in the Chinese context is "Cortana", which is actually a built-in function of the Windows 10 system. Cortana, an artificial intelligence (AIassistant) service program. Frequently asked questions and solutions. How to open Cortana and not respond. Solution steps. Chinese solution is not supported. How to put the search box into Cortana. What software is Cortana? Answer: "Cortana" It is a cloud platform personal intelligent assistant carefully built by Microsoft. It has two usage modes: login and non-login. When you are logged in
