Introduction: This is a detailed page of the session in PHP, introduced and PHP, related knowledge, skills, experience, and some PHP source code and so on.
class= ' pingjiaf ' frameborder= ' 0 ' src= ' http://biancheng.dnbcw.info/pingjia.php?id=324031 ' scrolling= ' no ' > overview
Session refers to the time that a user has spent browsing a website, from entering the website to the browser closing, which is the time it takes for the user to browse the site. From the definition above, we can see that the session is actually a specific time concept.
In general, in a page on the site of a variable (referring to server-side variables, the same below) is not available on the next page, with the session is good to run. Variables registered in the session can be used as global variables. This allows us to use the session for user authentication, program status logging, and parameter passing between pages.
How is the session implemented in the PHP3 version?
PHP3 itself is not implemented session function, we only use other methods to achieve, this one of the most famous to calculate phplib. Phplib's most basic functions include user authentication, session management, permissions, and abstraction of the database. Let's talk about how to implement the session with Phplib.
1, first install Phplib (Environment Win2000 php3.0.16 Apache1.3.12 phplib7.2c mysql3.23.21 for Win32)
First untie the Phplib, there is a "PHP" directory, the directory is copied 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 the files and directories in the Phplib directory (excluding the directory itself) to D:\Apache\htdocs.
Phplib class Library needs to be initialized 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 machine.
Change a program in the d:\Apache\php\prepend.php file to look like this:
if (!isset ($_phplib) or!is_array ($_phplib)) {
$_phplib["Libdir"] = "d:/apache/php/"; Put phplib the path to the PHP directory
}
To modify the D:\Apache\php\local.inc file:
Class Db_example extends Db_sql {
var $Host = "localhost"; MySQL Database host name
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 under the Phplib directory.
Since each page that uses phplib must first find the class library file necessary to run Phplib, we can set the Auto_prepend variable in php.ini to support it, Phplib contains a prepend.php file, and Auto_ Prepend specified as "d:/apache/php/prepend.php" (quoted), each page will automatically include the Phplib class library, we can also add the Phplib Class Library directory in the include variable so that the files can be found.
2. Call the Page_open () function
In each page that uses phplib, you must first call the Page_open function for initialization, for example:
Page_open (Array ("Sess" => "test_session"));
? >
The array variable (sess) is used to initialize some state-saving objects, which should be noted here: You must use the Phplib built-in name (Sess), which is defined in Local.inc.
Because Phplib uses cookies to store state information, the Page_open () function must be called before the page content is output to the browser. The PHP script should end with Page_close (), which will write the state data back to the database, or the variable will be lost.
3, specific use.
After registering a variable, you can use it on subsequent pages until the session ends. Method:
register ("varname");
Note that the varname 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 accesses the variable to get the changed value. The type of the variable is varied and can be a string, a number, an array. For example:
First page:
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:
Page_open ();//Start session
echo $welcome;//Display the $welcome defined in the first page
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, which would have unpredictable consequences. When the variable is used and is no longer needed, you can call the following function to delete the variable:
Page_open (Array ("Sess" => "Test _session"));
......
$sess->unregister ("variable_name");
......
Page_close ();
? >
How is the session 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 hold 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 beginning of each page that needs to be used for the session, for example:
?
$welcome = "Hello world!";
Session_register ("Welcome");//Register $welcome variable, note that there is no $ symbol
if (session_is_registered ("Welcome"))//Check if the $welcome variable is registered
echo "Welcome variable has been registered!";
Else
echo "Welcome variable not yet registered!";
? >
Customization of Session processing in PHP4
We need to expand 6 functions:
Sess_open ($sess _path, $session _name);
This function is used by the session handler to initialize the work.
Parameter $sess_path corresponds to Session.save_path option in php.ini file
The parameter $session_name corresponds to the session.name option in the php.ini.
Sess_close ();
This function is called when the page finishes executing and the session handler needs to be closed
Sess_read ($key);
This function retrieves and returns session data identified as $key when the session handler reads the specified session key value ($key). (Note: Serialization is the technique of saving a variable or object in a file at the end of a program or when needed, and then into memory the next time the program runs or needs it, as opposed to a method that only saves the data.) )
Sess_write ($key, $val);
This function is called when the session handler needs to save the data, which often happens 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 needed 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 the fragments. In this case, it is responsible for deleting obsolete session data. Session handlers occasionally invoke them.
Custom programs can save session data with MySQL database or dbm files, depending on the 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 $sess_db* variable of the session_mysql.php file to match the database settings on your machine:
?
$SESS _dbhost = "localhost"; /* Database host name */
$SESS _dbname = "Sessions"; /* Database name */
$SESS _dbuser = "Phpsession"; /* Database user name */
$SESS _dbpass = "Phpsession"; /* Database Password */
$SESS _DBH = "";
$SESS _life = Get_cfg_var ("Session.gc_maxlifetime");
...//Custom functions
Session_set_save_handler ("Sess_open", "Sess_close", "Sess_read", "Sess_write", "Sess_destroy", "sess_gc");
? >
To customize the interface when using dbm files:
?
$SESS _dbm = "";
$SESS _life = Get_cfg_var ("Session.gc_maxlifetime");
...//Custom functions
Session_set_save_handler ("Sess_open", "Sess_close", "Sess_read", "Sess_write", "Sess_destroy", "sess_gc");
? >
Session Custom Test Code:
......
if ($handler = = "dbm") include ("session_dbm.php");//what interface to use
ElseIf ($handler = = "MySQL") include ("session_mysql.php");
else ...
Session_Start ();
Session_register ("Count");
......
? >
In the authentication, how to apply the session?
Session can be used for user authentication:
Verify that the user is legitimate:
?
Session_Start ();
...//validation process
Session_register ("Reguser");
? >
Check whether a user is logged on in another page
?
Session_Start ();
if (Isset ($reguser) && $reguser! = "") {//If you are already signed in
echo "Dear User, welcome you";
}else{//If you are not logged in
echo "Please register first!" ";
}
? >
User logged out:
?
Session_destroy ();
......
? >
How to implement multi-session concurrent operation?
Question: I am writing a invoicing system for my organization and I find it necessary to allow multiple users to enter a PHP application at the same time. The original design of the static unique session ID leads to data confusion. This makes it imperative to dynamically generate a unique session ID.
The solution is simple: I use the PHP file name timestamp as the unique session ID, so that in my program each session is in position, no longer chaotic.
Below put my source code published, convenient also have the same problem friend more a 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 notes:
Use Mysessionname to transfer variables to the unique sessionname between pages, and if you use that name you must make a small change to the above procedure. Mysessionname cannot be the internal variable name of the session because he already exists before the session begins. Mysessionname also cannot be stored in cookies because multiple sessions will definitely overwrite the original cookie file. You can save it by using the domain of the implied form. This will not be a problem.
More articles on "The Session in PHP"
Love EE focus on Java Michael Jackson Video station JSON online tool
Http://biancheng.dnbcw.info/php/324031.html pageno:15