Session refers to the time when a user browses to a Web site, from entering a Web site to a browser shutdown, which is the time the user spends browsing the site. As we can see from the above definition, the session is actually a specific time concept.
Generally speaking, the variable in a certain page on the website (refers to the server-side variables, the same below) can not be used in the next page, with the session will be good to do. Variables registered in the session can be used as global variables. In this way we can use the session for user authentication, program status record, and parameter transfer between pages.
How does the session be implemented in the PHP3 version?
PHP3 itself does not implement the session function, we only use other methods to achieve, which is the most famous of the phplib. The most basic functions of phplib include user authentication, session management, permissions, and abstraction of the database. Let's tell you how to implement session with Phplib.
1. Install Phplib First (environment win2000+php3.0.16+apache1.3.12+phplib7.2c+mysql3.23.21 for Win32)
First unlock the Phplib, which has a "PHP" directory that copies the directory to the Apache installation directory. For example, if Apache is installed in the D:\Apache directory, copy the "PHP" directory to D:\Apache and copy files and directories under the pages directory of the Phplib directory (excluding the directory itself) to D:\Apache\htdocs.
Phplib class libraries need to initialize according to the system, may need to modify the Local.inc file, which contains some basic parameters, can be modified according to the actual situation of their own machines.
Change a procedure in the d:\Apache\php\prepend.php file as follows:
if (!isset ($_phplib) or!is_array ($_phplib)) {
$_phplib["Libdir"] = "d:/apache/php/"; Put phplib the path of the PHP directory
}
To modify the D:\Apache\php\local.inc file:
Class Db_example extends Db_sql {
var $Host = "localhost"; Host name of MySQL database
var $Database = "Test"; Database name
var $User = "root"; Database user Name
var $Password = "1234567"; Database user Password
}
Finally, the initial table is generated based on the Create_database.mysql file in the stuff subdirectory of the Phplib directory.
Since every page that uses phplib must first find the class library files necessary to run Phplib, we can set the Auto_prepend variable in php.ini to support it, Phplib contains a prepend.php file, and will Auto_ Once the prepend is specified as "d:/apache/php/prepend.php" (quoted), the pages automatically contain the Phplib class library, and we can also add the directory of the Phplib class library to the include variable so that the files can be found.
2, call the Page_open () function
In every page that uses phplib, you must first invoke the Page_open function for initialization, for example:
The array variable (sess) is used to initialize some state-saving objects, which should be noted here: Phplib built-in names (Sess) must be used, which are defined in Local.inc.
Because Phplib uses cookies to hold state information, the Page_open () function must be invoked before the page content is exported to the browser. The PHP script should end with Page_close (), which will write the state data back to the database, otherwise the variable will be lost.
3, the specific use.
After registering a variable, you can use it on subsequent pages until the session ends. Method:
<?php $sess->register ("varname");
Note that the varname 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 will get a change when it accesses the variable. 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" => "Test _session"));
$sess->register ("Welcome"); Register variable $welcome, note that you do not need to add $
$welcome = "hello,php world!";
......
Page_close ();
? >
Second page:
<?php
Page_open ()/Start session
echo $welcome//Displays the $welcome defined on the first page
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 is no longer needed, you can call the following function to delete the variable:
How does the session be implemented in the PHP4 version?
PHP4 's session also relies on cookies to save the session ID, save the variable with the file system (by default), so its session variable cannot save the object. Of course, you can also save the session in the database.
There are a lot of functions about session in PHP4 (see php.ini configuration article), usually we only need to call three functions: Sesssion_start (), Session_register (), Session_is_ Registered ().
Call the Session_Start () function at the very beginning of each page where you need to use the session, for example:
<?session_start ()? > >
?
$welcome = "Hello world!";
Session_register ("Welcome")//Register $welcome variable, note that there is no $ symbol
if (session_is_registered ("Welcome"))//Check whether the $welcome variable is registered
echo "Welcome variable has been registered!";
Else
echo "Welcome variable has not yet been registered!";
? >
</body>
Customization of Session processing in PHP4
We need to expand 6 functions:
Sess_open ($sess _path, $session _name);
This function is called by the session handler to initialize the work.
Parameter $sess_path corresponds to the Session.save_path option in the php.ini file
The parameter $session_name corresponds to the session.name option in the php.ini.
Sess_close ();
This function is invoked at the end of the page and when the session handler needs to be closed
Sess_read ($key);
This function retrieves and returns the session data identified as $key when the session handler reads the specified session key value ($key). (Note: Serialization is a technique for storing a variable or object in a file at the end of a program or when it is needed, in the next time the program runs or needs to be transferred into memory, as opposed to a method of saving only data.) )
Sess_write ($key, $val);
This function is called when the session handler needs to save the data, which often occurs at the end of the program. It is responsible for storing the data in the next place that can be retrieved using the Sess_read ($key) function.
Sess_destroy ($key);
This function is required to destroy the session. It is responsible for deleting the session and clearing the environment.
SESS_GC ($maxlifetime);
This function is responsible for cleaning up fragmentation. In this case, it is responsible for deleting obsolete session data. The session handler calls them occasionally.
Custom programs can use MySQL database or dbm file to save session data, depending on the specific situation. If you use MySQL for support, the following steps are required:
First, create a sessions database in MySQL and create a sessions table:
mysql> CREATE DATABASE sessions;
mysql> GRANT Select, INSERT, UPDATE, delete on sessions.* to Phpsession@localhost
-> identified by ' phpsession ';
mysql> CREATE TABLE Sessions (
-> Sesskey char (not NULL),
-> Expiry int (one) unsigned NOT NULL,
-> Value text NOT NULL,
-> PRIMARY KEY (Sesskey)
->);
Next, modify the session_mysql.php file's $sess_db* variable to match the database settings on your machine:
?
Session_Start ();
.//Verification process
Session_register ("Reguser");
? >
Check to see if a user is logged on in another page
?
Session_Start ();
if (Isset ($reguser) && $reguser!= "") {//if already logged in
echo "Dear User, welcome you";
}else{//If you are not logged in
echo "Please register first!" ";
}
? >
User Exits Login:
?
Session_destroy ();
......
? >
How do I implement multiple session concurrent running?
Question: I am writing a invoicing system for the organization that requires multiple users to enter a PHP application at the same time. The original static unique session ID of the design resulted in data clutter. This makes it imperative to dynamically generate a unique session ID.
The solution is simple: I used the PHP filename + timestamp as the only session ID, so that every session in my program is in place and no longer confusing.
Below put my source code published, convenient also has the same problem friend one more solution.
Start a PHP session to preserve variables.
if (empty ($mysessionname)) {
$micro = Microtime ();
$micro = Str_replace ("", "", $micro); Strip out the blanks
$micro = Str_replace (".", "", $micro); Strip out the periods
$mysessionname = "Po_maint". $micro;
}
Session_name ($mysessionname);
Session_Start ();
Program Comment:
Use Mysessionname for the only sessionname transfer variable between pages, and if you use that name you have to make a small change to the program. Mysessionname cannot be the internal variable name for the session because he already existed before the session started. Mysessionname also cannot be stored in cookies, as multiple sessions will certainly overwrite the original cookie file. You can save it with an implied form field. So there's no problem.
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.