Home php教程 php手册 session全教程(一)

session全教程(一)

Jun 13, 2016 am 10:13 AM
session one Meeting I Tutorial what is Expect dictionary


session是什么,刚开始我也不明白,非专业词典翻译为会议,会议期。作个不太恰当的比喻吧
(虽然不恰当,但意义却是一样的),session是你和网站之间的感情。session在WEB技术中占有非常重要的份量。由于网页是一种无状态的连接程序,因此你无法得知用户的浏览状态。因此我们必须
通过session记录用户的有关信息,以供用户再次以此身份对web服务器提供要求时作确认,例
如,我们在某些网站中常常要求用户登录,但我们怎么知道用户已经登录了呢,如果没有session的话,登录信息是无法保留的,那岂不要让用户在每一页网页中都要提供用户名和密码。


当然,session不光用于用户身份认证功能,还可能用于其它方面,以后我们会提到的。session用中文来解释就是会话期。一个会话期开始于用户输入一个站点的网址时,结束于他离开这个站点时。session最早出现在动态脚本语言ActiveServerPages中,它的功能之强大,是一句话无法说清楚的。


当php还在3.0版本时,session是它永远的痛。虽然php具有执行速度快,使用灵活,功能强大等优点,但因为session的问题,使很多站点的开发放弃了php,至少我的老板是这样认为的。当时有很多php免费函数库提供在php3上实现session的方案,但都让人感觉不正宗。就好象你花好几千大洋买的手机却配置一个很粗糙的草作的袋子一样,虽然功能是一样的,但总让人觉得别扭。php4的出现让PHP在session问题上有了翻身的机会。虽然它的session实现还不是很理想(主要是效率问题),但毕竟是它自己实现的,而且可以实际使用了。那我们用session干什么呢,你说了半天,我用不上的话,你岂不有卖纸张之嫌。OK,我们来看看session有什么用:作过网站的人都有这样的体会,在一页页面中的变量(在本章都指服务器端变量,下同)是不能在下一页中用的,虽然有一些办法可以实现,比如用form,urlstring等等,但有些对于用户来说是不方便的,即使让form自动提交,但其中的延时在现今的网络状况下足以让人窒息,而这两种方法都明显加大程序员的负担。如果你正在开发一个大型项目,那这些额外的负担是不能忽略的。而有了session就好办了,session中注册的变量可以作为全局变量使用。什么,全局变量?好极了。这样一来,你知道有什么用了吧:最主要的用于用户身份认证,程序状态记录,页面之间参数传递。


说了它这么半天的好处,你已经动心了吧,先别高兴,它还有缺点呢:它是用文件保存的变量(当然效率不高了,虽然可以用别的方式,但很麻烦的),不能保存对象。与之相对的是,asp中的session可以保存对象变量,用内存变量来保存session变量。但为什么我们还选用php呢,呵呵,为什么,你能从本书的开始看到这章,想必你也应该明白了吧,你还不明白,Faint,你再从头看起吧,我保证你成为PHP专家^_^。


session是怎样实现的呢?呵呵,你一定以为很高深吧,我来告诉你它的秘密。如果说只保存变量的话,很多读者都明白,这是很简单的,但前面我们说过,http协议是一种无状态的连接,你怎么知道那个变量是谁的,这个变量又是谁的呢?在session实现中用cookie实现的。cookie存在于客户端,也就是用户的机器中,里面保存着用户的sessionID,也就是session号码,当用户的浏览器请求服务器时把sessionID也一起送到服务器,这样服务器就可以识别你是谁,也就可以把变量识别开了。这样我们就不难理解了,为什么有时session会失效了。不信的话,你可以试试:在IE的"工具"菜单上有"Internet选项"菜单,打开后再选"安全"->"自定义级别",将安全设置中的"允许使用每个对话cookies"设为禁用,再看看session能不能用。这下明白了吧!不过PHP4在linux/unix平台上可以自动检查cookies状态,当cookies不可用时,自动会把sessionID附带在url上进行传递。这是它在session方面比asp多的唯一的优点了。

 

Statement of this Website
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

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Tutorial on how to use Dewu Tutorial on how to use Dewu Mar 21, 2024 pm 01:40 PM

Dewu APP is currently a very popular brand shopping software, but most users do not know how to use the functions in Dewu APP. The most detailed usage tutorial guide is compiled below. Next is the Dewuduo that the editor brings to users. A summary of function usage tutorials. Interested users can come and take a look! Tutorial on how to use Dewu [2024-03-20] How to use Dewu installment purchase [2024-03-20] How to obtain Dewu coupons [2024-03-20] How to find Dewu manual customer service [2024-03-20] How to check the pickup code of Dewu [2024-03-20] Where to find Dewu purchase [2024-03-20] How to open Dewu VIP [2024-03-20] How to apply for return or exchange of Dewu

In summer, you must try shooting a rainbow In summer, you must try shooting a rainbow Jul 21, 2024 pm 05:16 PM

After rain in summer, you can often see a beautiful and magical special weather scene - rainbow. This is also a rare scene that can be encountered in photography, and it is very photogenic. There are several conditions for a rainbow to appear: first, there are enough water droplets in the air, and second, the sun shines at a low angle. Therefore, it is easiest to see a rainbow in the afternoon after the rain has cleared up. However, the formation of a rainbow is greatly affected by weather, light and other conditions, so it generally only lasts for a short period of time, and the best viewing and shooting time is even shorter. So when you encounter a rainbow, how can you properly record it and photograph it with quality? 1. Look for rainbows. In addition to the conditions mentioned above, rainbows usually appear in the direction of sunlight, that is, if the sun shines from west to east, rainbows are more likely to appear in the east.

Tutorial on how to turn off the payment sound on WeChat Tutorial on how to turn off the payment sound on WeChat Mar 26, 2024 am 08:30 AM

1. First open WeChat. 2. Click [+] in the upper right corner. 3. Click the QR code to collect payment. 4. Click the three small dots in the upper right corner. 5. Click to close the voice reminder for payment arrival.

What software is photoshopcs5? -photoshopcs5 usage tutorial What software is photoshopcs5? -photoshopcs5 usage tutorial Mar 19, 2024 am 09:04 AM

PhotoshopCS is the abbreviation of Photoshop Creative Suite. It is a software produced by Adobe and is widely used in graphic design and image processing. As a novice learning PS, let me explain to you today what software photoshopcs5 is and how to use photoshopcs5. 1. What software is photoshop cs5? Adobe Photoshop CS5 Extended is ideal for professionals in film, video and multimedia fields, graphic and web designers who use 3D and animation, and professionals in engineering and scientific fields. Render a 3D image and merge it into a 2D composite image. Edit videos easily

Meetings can now be held using DingTalk on WeChat Meetings can now be held using DingTalk on WeChat Mar 05, 2024 pm 03:10 PM

This site reported on March 4 that DingTalk, owned by Alibaba, has received the iOS version 7.5.10 update, which supports browsers to directly enter DingTalk meetings. The log shows that by clicking on the DingTalk video conference joining link in WeChat or an external browser, you only need to verify your mobile phone number to join the meeting, making the meeting more convenient. Tested by this site, the DingTalk meeting function in WeChat currently only has a few core functions such as switching the microphone, switching the camera, inviting friends, and viewing members. To join the meeting for the first time, you need to enter a mobile phone number verification code. If you enter the meeting again within the next 30 days, you can achieve the same goal. Key shortcut login. The following is the updated content of DingTalk iOS version 7.5.10: [Attendance punch-in: shift supports setting multiple break times] Supports the attendance punch-in shift function of setting multiple break times, which is suitable for long-term work,

Experts teach you! The Correct Way to Cut Long Pictures on Huawei Mobile Phones Experts teach you! The Correct Way to Cut Long Pictures on Huawei Mobile Phones Mar 22, 2024 pm 12:21 PM

With the continuous development of smart phones, the functions of mobile phones have become more and more powerful, among which the function of taking long pictures has become one of the important functions used by many users in daily life. Long screenshots can help users save a long web page, conversation record or picture at one time for easy viewing and sharing. Among many mobile phone brands, Huawei mobile phones are also one of the brands highly respected by users, and their function of cropping long pictures is also highly praised. This article will introduce you to the correct method of taking long pictures on Huawei mobile phones, as well as some expert tips to help you make better use of Huawei mobile phones.

PHP Tutorial: How to convert int type to string PHP Tutorial: How to convert int type to string Mar 27, 2024 pm 06:03 PM

PHP Tutorial: How to Convert Int Type to String In PHP, converting integer data to string is a common operation. This tutorial will introduce how to use PHP's built-in functions to convert the int type to a string, while providing specific code examples. Use cast: In PHP, you can use cast to convert integer data into a string. This method is very simple. You only need to add (string) before the integer data to convert it into a string. Below is a simple sample code

Honor mobile phone Hongmeng system upgrade tutorial Honor mobile phone Hongmeng system upgrade tutorial Mar 23, 2024 pm 12:45 PM

Honor mobile phones have always been favored by consumers because of their excellent performance and stable system. Recently, Honor mobile phones have released a new Hongmeng system, which has attracted the attention and expectations of many users. Hongmeng system is known as the system that "unifies the world". It has a smoother operating experience and higher security, allowing users to experience a new world of smartphones. Many users have expressed that they want to upgrade their Honor mobile phone system to the Hongmeng system. So, let’s take a look at the upgrade tutorial of the Honor mobile phone’s Hongmeng system. firstly, I

See all articles