PHP Custom File Save Session method, _php tutorial

Source: Internet
Author: User

PHP custom file to save the session method,


This article describes the PHP custom file Save Session method. Share to everyone for your reference. The implementation method is as follows:

session.inc.php file: Defines the session's file storage, the session solution is to provide a way to define global variables in the PHP script, so that the global variable in the same session for all PHP scripts are valid, we know that Session is not a simple concept of time, a session also includes a specific user and server, so in more detail, in a session defined by the scope of the global variables, refers to the session of the corresponding user access to all of the PHP, For example, a user defines a global variable $user= "Wind" through the session, and the B user $user= "Jane" through the global variable defined by the session, then the $user value in the PHP script that a user accesses is wind.

How PHP Creates a session

Start by describing how to create a session, very simple, start session sessions, and create a $admin variable:

Start session:

Session_Start ();

Declare a variable named admin and assign a null value: $_session["admin"] = null;

If you use Seesion, or the PHP file to invoke the session variable, then you must start it before calling the session, use the Session_Start () function, the others do not need you to set up, PHP automatically complete the session file creation, execution finished After this program, we can go to the system Temp folder to find this session file, the general file name is like: Sess_4c83638b3b0dbf65583181c2f89168ec, followed by a 32-bit encoded random string, open it with an editor and look at its contents: The admin|n;,php instance code is as follows:
Copy CodeThe code is as follows: <?php
Defining a hyper-global array
$_session = Array ();
Defining file Handles
$fp = null;

User-defined open session function
function Session_file_start () {

1. First determine if the browser has sent a cookie value
if (Isset ($_cookie[' Fileid ')) {

2. Receive Cookie Value
$filename = $_cookie[' Fileid ');

3. Open file for read-write
if (file_exists ($filename)) {
$globals [' fp '] = fopen ($filename, ' r+ ');
} else {
$globals [' fp '] = fopen ($filename, ' w+ ');
}
} else {
2. Set a file and put the file name in a cookie
$filename = Date (' Ymdhis ');
Setcookie (' Fileid ', $filename, Time () +60*60*24);

3. Open file for read-write
$globals [' fp '] = fopen ($filename, ' w+ ');

}//end of If-else

4. Storing the data in a file in a hyper-global array $_session
while (!feof ($globals [' FP '])) {
Read a line in a file
$buffer = fgets ($globals [' FP ']);
Handle the line that is read
$tmparr = explode (' = ', trim ($buffer, ' RN '));

Added to the session array
if (count ($tmparr) = = 2) {
$globals [' _session '] [$tmparr [0]] = $tmparr [1];
}
}//end of While

}//end of Session_file_start ()

Functions that register session variables
function Session_file_register ($key, $val) {

Set Session Variables
$globals [' _session '] [$key] = $val;

Put the variable in a 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 NULL
$globals [' _session '] = array ();
}//end of Session_file_destroy ()
Test code file: 1.php
Copy CodeThe code is as follows: <?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 Session Array
echo $_session[' username '];
?>
Next page
Test file: 2.php
Copy CodeThe code is as follows: <?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 '];
?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/925130.html www.bkjia.com true http://www.bkjia.com/PHPjc/925130.html techarticle PHP Custom file Saving session method, this article describes the PHP custom file saving session method. Share to everyone for your reference. The specific implementation method is as follows: session.inc.php file ...

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