Home php教程 php手册 session全教程

session全教程

Jun 21, 2016 am 09:10 AM
apache php quot session

session|教程

一、session概述

session是什么,刚开始我也不明白,非专业词典翻译为会议,会议期。作个不太恰当的比喻吧(虽然不恰当,但意义却是一样的),,session是你和网站之间的感情。
session在WEB技术中占有非常重要的份量。由于网页是一种无状态的连接程序,因此你无法得知用户的浏览状态。因此我们必须
通过session记录用户的有关信息,以供用户再次以此身份对web服务器提供要求时作确认,例如,我们在某些网站中常常要求用户登录,
但我们怎么知道用户已经登录了呢,如果没有session的话,登录信息是无法保留的,那岂不要让用户在每一页网页中都要提供
用户名和密码。
当然,session不光用于用户身份认证功能,还可能用于其它方面,以后我们会提到的。
session用中文来解释就是会话期。一个会话期开始于用户输入一个站点的网址时,结束于他离开这个站点时。
session最早出现在动态脚本语言Active Server Pages中,它的功能之强大,是一句话无法说清楚的。
当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
存在于客户端,也就是用户的机器中,里面保存着用户的session ID,也就是session号码,当用户的浏览器请求服务器时把session ID也一起送到
服务器,这样服务器就可以识别你是谁,也就可以把变量识别开了。这样我们就不难理解了,为什么有时session会失效了。不信的话,你可以试试:
在IE的"工具"菜单上有"Internet选项"菜单,打开后再选"安全"->"自定义级别",将安全设置中的"允许使用每个对话cookies"设为禁用,再看看
session能不能用。这下明白了吧!不过php4在linux/unix平台上可以自动检查cookies状态,当cookies 不可用时,自动会把session ID
附带在url上进行传递。这是它在session方面比asp多的唯一的优点了。

二、php3,4中session的实现

在php3中是没有session这种东东的,但我们又需要,怎么办呢?别急,有很多人替你做了这些,这其中最有名的要算phplib了。你可以去国外下
载,也可以上国内大部分php站点下载。我们要做的第一件事是让phplib和php3结合在一起使它能工作。为了能实现这方面的功能,我们需要先安装phplib。
跟着我来做,很容易的(以下方法在win2000+php3.0.16+apache1.3.12+phplib7.2c+mysql3.23.21 for win32 上通过)
phplib最基本的功能包括用户认证,Session管理,权限及数据库的抽象化。
怎样使用phplib来实现session功能呢?
一、首先你将phplib解开,里面有一个目录叫"php",将这个目录拷贝到apache的安装目录下。以下以笔者的机器为例:
我的apache安装在d:apache 目录下,我将上面的"php"目录拷贝到d:apache,并将phplib下的pages目录下的文件和目录一起拷贝到
d:apachehtdocs下,注意不带目录本身。
phplib的类库需要根据系统进行初始化,你可以修改local.inc文件,其中包含着一些基本参数,你可以根据自己机器的实际情况来进行修改。
将d:apachephpprepend.php3文件中的一段程序改为如下样子:

if (!isset($_PHPLIB) or !is_array($_PHPLIB)) {
$_PHPLIB["libdir"] = "d:/apache/php/"; //这儿改为你放phplib下php目录的路径
}

然后将d:apachephplocal.inc文件改如下:

class DB_Example extends DB_Sql {
var $Host = "localhost";//你的mysql数据库所在主机名
var $Database = "test";//数据库名
var $User = "root";//数据库用户名
var $Password = "";//数据库用户口令
}

最后一步执行解开的phplib目录中的stuff目录下的create_database.mysql文件,生成初始表。
我们说明一下phplib的工作原理,每一个使用phplib的页面首先必须可以找到运行phplib所必须类库文件,我们可以在php3.ini中设置auto_prepend
变量来支持,phplib分发包中包含一个prepend.php3文件,将auto_prepend指定为"d:/apache/php/prepend.php3"(带引号)后,各页面就会自动包含phplib类库,我们还可以
将phplib类库所在目录加进include变量中,以便可以找到这些文件,当然,最苯的办法就是指定phplib的绝对路径,这可不是个好主意,可移植性太差!
第二步,每一个使用phplib的页面中,你必须首先调用page_open函数进行初始化。这会告诉phplib,你现在或将来会用到状态保存。一个典型的
page_open例子如下:



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)

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

How to set the cgi directory in apache How to set the cgi directory in apache Apr 13, 2025 pm 01:18 PM

To set up a CGI directory in Apache, you need to perform the following steps: Create a CGI directory such as "cgi-bin", and grant Apache write permissions. Add the "ScriptAlias" directive block in the Apache configuration file to map the CGI directory to the "/cgi-bin" URL. Restart Apache.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

How to start apache How to start apache Apr 13, 2025 pm 01:06 PM

The steps to start Apache are as follows: Install Apache (command: sudo apt-get install apache2 or download it from the official website) Start Apache (Linux: sudo systemctl start apache2; Windows: Right-click the "Apache2.4" service and select "Start") Check whether it has been started (Linux: sudo systemctl status apache2; Windows: Check the status of the "Apache2.4" service in the service manager) Enable boot automatically (optional, Linux: sudo systemctl

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

What to do if the apache80 port is occupied What to do if the apache80 port is occupied Apr 13, 2025 pm 01:24 PM

When the Apache 80 port is occupied, the solution is as follows: find out the process that occupies the port and close it. Check the firewall settings to make sure Apache is not blocked. If the above method does not work, please reconfigure Apache to use a different port. Restart the Apache service.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

How Debian improves Hadoop data processing speed How Debian improves Hadoop data processing speed Apr 13, 2025 am 11:54 AM

This article discusses how to improve Hadoop data processing efficiency on Debian systems. Optimization strategies cover hardware upgrades, operating system parameter adjustments, Hadoop configuration modifications, and the use of efficient algorithms and tools. 1. Hardware resource strengthening ensures that all nodes have consistent hardware configurations, especially paying attention to CPU, memory and network equipment performance. Choosing high-performance hardware components is essential to improve overall processing speed. 2. Operating system tunes file descriptors and network connections: Modify the /etc/security/limits.conf file to increase the upper limit of file descriptors and network connections allowed to be opened at the same time by the system. JVM parameter adjustment: Adjust in hadoop-env.sh file

See all articles