Session full Tutorial (ii) _php tutorial

Source: Internet
Author: User
Tags md5 encryption session id php3 file random seed
Second, the realization of the session in php3,4

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

How to use Phplib to implement session function?

First, you will phplib untie, there is a directory called "PHP", the directory is copied 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 above "PHP" directory to D:a/pache, and Phplib under the pages directory under the file and the directory is copied to D:/apache/htdocs, Note that the directory itself is not taken. 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 according to the actual situation of your machine to modify. Will d:/apache/php/prepend. A procedure in the PHP3 file is changed to look like this:

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

Then change the D:/apache/php/local.inc file as follows:

Class Db_example extends Db_sql {
var $Host = "localhost";//host name of your MySQL database
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 that was untied. MySQL file to generate the initial table. We explain how phplib works, each page that uses phplib must first find the class library file that runs Phplib, and we can set the Auto_prepend variable in Php3.ini to support it. The phplib bundle contains a prepend.php3 file that will automatically include the Phplib class library when the auto_prepend is specified with "D:/apache/php/prepend.php3" (quoted). We can also add the directory of the Phplib class library into the include variable so that these files can be found, of course, the most benzene way is to specify the absolute path of phplib, this is not a good idea, portability is poor!

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

Page_open (Array ("sess" = "example_session"));
?>

The array variable (sess) is used to initialize some state-saving objects, note that you must use the Phplib built-in name (Sess), which is defined in Local.ini, and the Page_open function must be called before the page content is output 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 store state information, the Page_open () function must be called before the page content is output to the browser, where the page content can be any HTML information or blank line, if you find the error "Oops-setcookie Called after the header has been sent ", which indicates what was output to the browser before Page_open (), you should pay special attention to the empty line because it is very difficult to find, the typical mistake is to output a blank line between the tags, You should check if the Local.inc and prepend.php3 files contain blank lines, 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, specific use.
When a user visits the site, the user's session begins, and if the user's browser supports cookies, a session ID is placed into the cookie, and the unique ID is randomly generated by PHP3. And then using the random seed string for MD5 encryption, the cookie here should be called session cookie, because this cookie is not written to the user's hard drive, and when a session end, the cookie is also ended. If the user browser does not support cookies, then the ID of the session will be placed in the URL chain, because it is encrypted, so it is useless to steal. Session ID holds information about the user, such as user authentication, authentication expiry time, user rights, and other information you may need to facilitate our access. The session is actually the process of the user's sessions. The session is not just used to track the user's registration, in fact, it can also have other uses, you can use it to store any information you want to storage, this information can be used in the page that the user subsequently visited, of course, if those pages to use Phplib. The method is simple, register a variable and then use it on subsequent pages until the session ends. Method:
Register ("Variable_name");?>

Note that the variable_name here is not a variable value, but a variable name, you can specify the variable name first, and then assign the value. You can change the value of a variable in a page, and the subsequent page access to the variable will have the value changed. The type of the variable is varied and can be a string, a number, an array. For example:

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

Second page:
Page_open ();//Start session


echo $first;//Look at the effect

Page_close ();//Save state information
?>

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

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

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

http://www.bkjia.com/PHPjc/629568.html www.bkjia.com true http://www.bkjia.com/PHPjc/629568.html techarticle Second, php3,4 in the implementation of the session in the PHP3 is not the session of this stuff, but we need, how to do? Don't worry, there are a lot of people doing this for you, the most famous of which is to calculate ...

  • 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.