Session full Course (ii)

Source: Internet
Author: User

The realization of session in php3,4

In PhP3 there is no session of this stuff, but we need, how to do? Don't worry, a lot of people do this for you, the most famous one is Phplib. You can go abroad to download, you can download most of the domestic PHP site. The first thing we have to do is get phplib and php3 together to make it work. In order to achieve this function, we need to first install Phplib. Follow me, it's easy (the following methods are passed on the win2000+php3.0.16+apache1.3.12+phplib7.2c+mysql3.23.21 for Win32) Phplib most basic functions include user authentication, session management , permissions and the abstraction of the database.

How to use Phplib to implement the session function?

First, you will phplib untie, there is a directory called "PHP", this directory copy to the Apache installation directory. Take the author's machine as an example: My Apache is installed in the D:/apache directory, I copy the "PHP" directory above to the D:a/pache, and copy the files and directories under Phplib under the pages directory to D:/apache/htdocs. Note that the directory itself is not taken. The Phplib class library needs to be initialized according to the system, you can modify the Local.inc file, which contains some basic parameters, you can modify it according to the actual situation of your machine. Will d:/apache/php/prepend. A procedure in the PHP3 file is changed as follows:

if (!isset ($_phplib) or!is_array ($_phplib)) {
$_phplib["Libdir"] = "d:/apache/php/"; Here instead you put phplib the path of the PHP directory
}

The D:/apache/php/local.inc file is then changed as follows:

Class Db_example extends Db_sql {
var $Host = "localhost";//your MySQL database host name
var $Database = "Test";//Database name
var $User = "root";//Database user name
var $Password = "";//Database user password
}

The final step is to perform the create_database in the stuff directory in the Phplib directory. MySQL file, generating the initial table. Let's explain how phplib works, every page that uses phplib must first find the class library file that runs Phplib, we can set the Auto_prepend variable to support in Php3.ini, A prepend.php3 file is included in the Phplib distribution, and the page automatically contains the Phplib class library when the auto_prepend is specified with the "d:/apache/php/prepend.php3" (quoted number). We can also add the Phplib Class Library directory into the include variable so that we can find these files, of course, the most benzene way is to specify phplib absolute path, this is not a good idea, portability is too bad!

In the second step, each page that uses phplib, you must first invoke the Page_open function for initialization. This will tell Phplib that you will use state saving now or in the future. A typical
Page_open examples are as follows:

? Php
Page_open (Array ("Sess" => "example_session"));
?>

The array variable (sess) is used to initialize some state-saving objects, noting that you must use the Phplib built-in name (Sess), which you define in Local.ini, and the Page_open function must be invoked before the page content is exported to the browser. PHP3 script should end with Page_close (), which will write the state data back to the database, if you forget, the result you should be able to think, haha, your variables are all lost, not to blame I didn't tell you ...

Because Phplib uses cookies to hold state information, the Page_open () function must be called before the page content is exported to the browser, where the page content can be any HTML information or a blank line, if you find the error "Oops-setcookie Called after header has been sent ", which indicates that the output to the browser before Page_open (), you have to pay special attention to the empty line, because it is very difficult to find, the typical error is in the.? And? > tags output blank lines, you should check whether the Local.inc and prepend.php3 file contains a blank line, which is also a very error-prone place. To reduce the likelihood of errors, we can write the initialization program like this:
?
Page_open (Array ("Sess" => "example_session"));
?>

.....

The third step, the concrete use.
When a user accesses the Web site, the user's session begins, and if the user's browser supports cookies, a session ID is placed in the cookie, and the unique ID is randomly generated by PHP3, And then using random seed strings for MD5 encryption, where the cookie should be called the session cookie, because the cookie is not written to the user's hard drive, when a session ends, the cookie is also completed. If the user's browser does not support cookies, then the session ID will be placed in the URL chain, because it is encrypted, so it is useless to steal it. The session ID holds information about the user, such as user authentication, authentication expiration, user rights, and other information you may need to facilitate our access. Session is actually the process of a user conversation. The session is not just for tracking the user's registration, but it can also have other uses where you can use it to store any information you want to save, which can be used in pages that the user subsequently accesses, assuming that those pages use Phplib. The method is simple enough to register a variable to use it on subsequent pages until the session ends. Method:
? PHP $sess->register ("variable_name");?>

Note that the variable_name is not a variable value, but a variable name, and you can specify the variable name and then assign the value. You can change the value of a variable on a page, and the subsequent page accesses the variable to get the changed value. The types of variables are diverse, and can be a string, a number, an array. For example, Ming:

First page:
? Php
Page_open (Array ("Sess" => "example_session"));
$sess->register ("a"); Note that you do not need to add $ before the variable name
if (Iset ($firstname)) {
$first = $firstname;
}
.....
Page_close ();
?>

Second page:
? Php
Page_open ()/Start session


echo $first/See effect

Page_close ()//Save state information
?>

After registering a variable, when the page finally calls the Page_close () function, each session variable is written back to the database. If you forget to call the Page_close () function, the variables will not be written back to the database, which can have unpredictable consequences. When a variable is used and you no longer need to use it, you can call the following function to delete the variable:

? Php
Page_open (Array ("Sess" => "example_session"));
...
$sess->unregister ("variable_name");
...
Page_close ();
?>

In Phplib 7.0, a storage structure is used that allows you to store session data in a database, in shared memory, or in LDAP. Phplib uses database classes, which gives you more options, and you can use Oracle8,mysql,postgresql and so on to save state information.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.