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

session全教程(二)

Jun 13, 2016 am 10:13 AM
session two exist accomplish I Tutorial yes of

二、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:a/pache,并将phplib下的pages 目录下的文件和目录一起拷贝到 d:/apache/htdocs下,注意不带目录本身。phplib的类库需要根据系统进行初始化,你可以修改local.inc文件,其中包含着一些基本参数,你可以根据自己机器的实际情况来进行修改。 将d:/apache/php/prepend.PHP3文件中的一段程序改为如下样子:

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

然后将d:/apache/PHP/local.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例子如下:

page_open(array("sess" => "Example_session"));
?>

数组变量(sess)用来初始化一些状态保存对象,注意:必须使用phplib内置名(sess),这些内置名是你在local.ini中所定义的,page_open函数必须在页面内容输出到浏览器之前被调用。PHP3脚本最后应以page_close()结束,这将会将有关状态数据写回到数据库中,如果你忘了的话,结果你应该能想到,哈哈,你的变量全丢了,可不要怪我没告诉你...

因为phplib使用了Cookies来保存状态信息,所以page_open()函数必须在页面内容输出到浏览器之前被调用, 这里的页面内容可以是任何HTML信息或者空行,如果你发现了错误"Oops - SetCookie called after header has been sent",这表明在page_open()之前向浏览器输出了些什么,你要特别留意空行,因为非常难找到,典型的错误是在 和 ? >标记之间输出了空行,你应检查在local.inc和prepend.PHP3文件中是否包含了空行,这也是一个非常容易出错的地方。为了减少出错的可能,我们可以这样书写初始化程序:

page_open(array("sess" => "Example_session"));
?>

.....

第三步,具体使用。
当一个用户访问了该网站后,随即用户的session就开始了,如果用户的浏览器支持cookie的话,将会建立一个session的id放入cookie,这个唯一的ID是由PHP3随机生成,然后又用随机种子字串进行md5加密过了的,这里的cookie应该叫做session cookie,因为这个cookie是不会写到用户硬盘里去的,当一个session期结束的时候,该cookie也被完结了。如果用户浏览器不支持cookie的话,那么 该session的id将会放入url链中,因为是加密过的,所以窃取了也没用。session ID存放着用户的有关信息,如用户已认证、认证到期时间、用户权限,和其他一些你可能需要的信息,方便我们取用。Session其实就是用户一次会话的过程。session并不是仅仅用来跟踪用户的注册,实际上,它还可以有其它的使用场合,你可以用它来存储任何你想要存贮的信息,这些信息可以在用户随后访问的页面中派上用场,当然前提是那些页面要使用PHPLIB。方法很简单,注册一个变量后即可在随后的页面中使用它,直至session结束。方法:
register( "variable_name"); ?>

注意,这里的variable_name不是变量值,而是变量名,可以先指定变量名,随后再赋值。你在某个页面中可以改变变量的值,随后的页面访问该变量会得到改变后的值。变量的类型是多样的,可以是一个字串,一个数字,一个数组。举例来说明:

第一页:
page_open(array("sess" => "Example_session"));
$sess->register( "first"); //注意变量名前不需要加$
if (iset($firstname)) {
$first = $firstname;
}
.....
page_close();
?>

第二页:
page_open();//开始session


echo $first;//看看效果

page_close();//保存状态信息
?>

注册完一个变量,当页面最后调用page_close()函数后,各个session变量会被写回到数据库中。如果你忘记调用page_close()函数的话,变量就不会被写回数据库,将出现不可预知的后果。当变量被使用完毕,你不再需要用到时,可以调用以下函数将变量删除:

page_open(array("sess" => "Example_session"));
...
$sess->unregister( "variable_name");
...
page_close();
?>

PHPLIB 7.0中,使用了一种存储结构,它允许你存储session数据到数据库中、共享内存中或者LDAP中。PHPLIB使用了数据库类,这使得你有了更多的选择,你可以选用Oracle8,MySQL,postgresql等等数据库来保存状态信息。

 

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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

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.

PHP Programming Guide: Methods to Implement Fibonacci Sequence PHP Programming Guide: Methods to Implement Fibonacci Sequence Mar 20, 2024 pm 04:54 PM

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

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

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.

See all articles