PHP Custom File Save Session Method _php Tips

Source: Internet
Author: User
Tags php script

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

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

How PHP Creates a session

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

Start session:

Session_Start ();

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

If you use the seesion, or the PHP file to call the session variable, then you must start it before calling the session, use the Session_Start () function, others do not need you to set up, PHP automatically completes the session file creation, execution After 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, look at its contents: The admin|n;,php instance code is as follows:

Copy Code code as follows:
<?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
Copy Code code 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 the session array
echo $_session[' username '];
?>
<a href= "2.php" > next page </a>

Test files: 2.php
Copy Code code 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 will help you with your PHP program design.

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.