session.inc.php Tutorial Files: Defines the session file store, the session solution, is to provide in the PHP script to define the global variable method, make this global variable in the same session for all PHP script is valid. As we mentioned above, session is not a simple concept of time, and a session includes specific users and servers. So in more detail, the scope of the global variable defined in a session refers to all PHP that the user of the session accesses. For example, a user defines a global variable $user= "Wind" through the session, while the B user $user= "Jane" through the global variable defined by the session. So in the PHP script that a user accesses, the value of $user is wind. How PHP Creates a session
Begins to describe how to create a session. It's very simple, really. Start session sessions, and create a $admin variable://Start the session_start (); Declares a variable named admin and assigns null values. $_session["admin" = null; ?> If you use Seesion, or if the PHP file calls the session variable, you must start it before calling the session and use the Session_Start () function. Other do not need you to set up, PHP automatically completes the session file creation. After executing this program, we can go to the system temporary folder to find this session file, general file name like: Sess_4c83638b3b0dbf65583181c2f89168ec, followed by 32-bit encoded random string. Open it with the editor and look at its contents: admin|n;
<?php
Define a Super Global array
$_session = Array ();
Define file Handle
$fp = null;
User-defined open session function
function Session_file_start () {
1. First determine if the browser has sent cookie values
if (Isset ($_cookie[' Fileid ')) {
2. Receive Cookie Value
$filename = $_cookie[' Fileid '];
3. Open file for reading and writing
if (file_exists ($filename)) {
$globals [' fp '] = fopen ($filename, ' r+ ');
} else {
$globals [' fp '] = fopen ($filename, ' w+ ');
}
} else {
2. Set a file and place the file name in a cookie
$filename = Date (' Ymdhis ');
Setcookie (' Fileid ', $filename, Time () +60*60*24);
3. Open file for reading and writing
$globals [' fp '] = fopen ($filename, ' w+ ');
}//end of If-else
4. Storing data in a file in a $_session array
while (!feof ($globals [' FP '])) {
Read a row in a file
$buffer = fgets ($globals [' FP ']);
Process the Read line
$tmparr = explode (' = ', trim ($buffer, ' RN '));
Adding to the session array
if (count ($tmparr) = 2) {
$globals [' _session '] [$tmparr [0]] = $tmparr [1];
}
}//end of While
}//end of Session_file_start ()
Functions for registering session variables
function Session_file_register ($key, $val) {
Set session variable
$globals [' _session '] [$key] = $val;
Put the variable in the file
Fseek ($globals [' FP '], 0, seek_end);
Fwrite ($globals [' FP '], "$key = $valrn");
}//end of Session_file_register ()
End Session Variable
function Session_file_destroy () {
1. Close file pointer
Fclose ($globals [' FP ']);
$fp = null;
2. Set the session array to empty
$globals [' _session '] = array ();
}//end of Session_file_destroy ()
Test code file: 1.php
<?php
Determine the encoding format
Header (' content-type:text/html; Charset=utf-8 ');
Include ("session-file.php");
Test function:
Open session
Session_file_start ();
Registering Session Variables
$key = ' username ';
$val = ' LSL ';
Session_file_register ($key, $val);
Session_file_register (' username ', ' Lisa ');
Print the session array
echo $_session[' username '];
?>
<a href= "2.php" > next page </a>
Test files: 2.php
<?php
Determine the encoding format
Header (' content-type:text/html; Charset=utf-8 ');
Include ("session-file.php");
Test function:
Open session
Session_file_start ();
echo $_session[' username '];
?>