Function description for getting started with PHPsession 2 _ PHP Tutorial

Source: Internet
Author: User
Tags php3 file random seed
Function description 2 of PHPsession entry. There is no such thing as session in php3, but we need it again. what should we do? Don't worry. many people have done this for you. the most famous one is phplib. You can go abroad and there is no such thing as session in php3, but what should we do? Don't worry. many people have done this for you. the most famous one is phplib. You can download it from a foreign site or from most php sites in China. The first thing we need to do is to combine phplib and php3 to make it work. To achieve this, we need to install phplib first. It's easy to follow me (the following methods are passed on win2000 php3.0.16 apache1.3.12 phplib7.2c mysql3.23.21 for win32). phplib's basic functions include user authentication, Session management, permission, and database abstraction.

How to use phplib to implement the session function?
1. first, unbind phplib. there is a directory named "php" in it, and copy the directory to the installation directory of apache. Take the author's machine as an example: my apache is installed in the d:/apache Directory, and I copy the above "php" directory to d: a/pache, copy the files and directories under the pages directory under phplib to the d:/apache/htdocs Directory. Note that the directory is not included. 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 the class library based on the actual situation of your machine. Change a program in the d:/apache/php/prepend. php3 file as follows:
If (! Isset ($ _ PHPLIB) or! Is_array ($ _ PHPLIB )){
$ _ PHPLIB ["libdir"] = "d:/apache/php/"; // change the path to the php directory under phplib.
}
Modify 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 username
Var $ Password = ""; // database user Password
}
In the final step, execute the create_database.mysql file in the stuff directory in the unlocked phplib directory to generate the initial table. Let's explain how phplib works. every page using phplib must first find the class library file required to run phplib. we can set the auto_prepend variable in php3.ini to support it, the phplib distribution package contains a prepend. php3 file, specify auto_prepend "d:/apache/php/prepend. after php3 "(with quotation marks), the pages will automatically contain the phplib class library. we can also add the Directory of the phplib class library to the include variable to locate these files. of course, the most benzene method is to specify the absolute path of phplib. this is not a good idea. portability is too bad!

Step 2: on every page using phplib, you must first call the page_open function for initialization. This will tell phplib that you will use status save now or in the future. A typical example of page_open is as follows:
Page_open (array ("sess" => "Example_Session "));
?>
The array variable (sess) is used to initialize some state storage objects. note: you must use the phplib built-in name (sess). These built-in names are stored in the local. as defined in ini, the page_open function must be called before the page content is output to the browser. The php3 script should end with page_close (), which will write the relevant status data back to the database. if you forget it, you should be able to think of the result. haha, you have lost all your variables, but don't blame me for not telling you... because phplib uses Cookies to store status information, the page_open () function must be called before the page content is output to the browser. the page content here can be any HTML information or empty rows, if you find the error "Oops-SetCookie called after header has been sent", this indicates what is output to the browser before page_open (). pay special attention to empty rows, the typical error is that Empty lines are output between tags. you should check whether empty lines are included in the local. inc and prepend. php3 files, which is also a very error-prone place. To reduce the possibility of errors, we can write the initialization program as follows:
Page_open (array ("sess" => "Example_Session "));
?>

.....


Step 3: use it. When a user accesses the website, the user's session starts. if the browser supports cookies, a session id will be created and put into the cookie, this unique ID is randomly generated by PHP3 and then encrypted by md5 using a random seed string. the cookie here should be called session cookie, because the cookie is not written to the user's hard disk, when a session ends, the cookie is also completed. If the user's browser does not support cookies, the session id will be put into the url chain. because the session id is encrypted, it is useless to steal the session. Session ID stores user information, such as user authentication, authentication expiration time, user permissions, and other information you may need, which is convenient for us to use. A Session is a user's Session process. Session is not just used to track user registration. In fact, it can also be used in other scenarios. you can use it to store any information you want to store, this information can be used in subsequent pages, provided that PHPLIB is used for those pages. The method is simple. after registering a variable, you can use it on the subsequent page until the session ends. Method:
Register ("variable_name");?>

Note: Here, variable_name is not a variable value, but a variable name. you can specify a variable name before assigning a value. You can change the value of a variable on a page, and then access the variable on the page to get the changed value. Variable types are diverse. they can be a string, a number, and an array. For example:
Page 1:
Page_open (array ("sess" => "Example_Session "));
$ Sess-> register ("first"); // note that you do not need to add $
If (iset ($ firstname )){
$ First = $ firstname;
}
.....
Page_close ();
?>
Page 2:
Page_open (); // start session
Echo $ first; // Check the effect
Page_close (); // Save the status information
?>
After registering a variable, when the page finally calls the page_close () function, each session variable will be written back to the database. If you forget to call the page_close () function, the variable will not be written back to the database and unpredictable consequences will occur. When the variable is used up and you no longer need to use 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 to databases, shared memory, or LDAP. PHPLIB uses database classes, which gives you more options. you can choose oracle8, mysql, postgresql, and other databases to save status information.

Why? Don't worry. many people have done this for you. the most famous one is phplib. You can go abroad...

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.