Php custom file saving session Method

Source: Internet
Author: User
This article mainly introduces the methods for saving sessions in php custom files, describes the session creation and usage techniques in detail, and the corresponding range analysis, which has some reference value, for more information, see

This article mainly introduces the methods for saving sessions in php custom files, describes the session creation and usage techniques in detail, and the corresponding range analysis, which has some reference value, for more information, see

This example describes how to save a session for a php custom file. Share it with you for your reference. The specific implementation method is as follows:

Session. inc. php file: defines the session file storage. The session solution is to provide a method to define global variables in the php script, make this global variable effective for all php scripts in the same session. We all know that session is not a simple concept of time. A session also includes specific users and servers, therefore, in more detail, the scope of the global variables defined by a session refers to all php files accessed by users corresponding to this session, for example, user a defines a global variable $ user = "wind" Through session, while user B defines the global variable $ user = "jane" Through session ", in the php script accessed by user a, the value of $ user is wind.

How to Create a session in php

It is very simple to start a session 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 needs to call the session variable, you must start the session before calling it and use the session_start () function. You do not need to set any other parameters, php automatically creates the session file. After executing this program, we can find the session file in the temporary folder of the system. The general file name is sess_4c83638b3b0dbf65583181c2f89168ec, the following is a 32-bit encoded random string. Open it in the editor and check its content: admin | n;. The PHP instance code is as follows:

The Code is as follows:

<? Php
// Define a super Global Array
$ _ Session = array ();
// Define the file handle
$ Fp = null;

// User-Defined enable session Function
Function session_file_start (){

// 1. first determine whether the browser has sent the cookie value
If (isset ($ _ cookie ['fileid']) {

// 2. Receive cookie value
$ Filename = $ _ cookie ['fileid'];

// 3. open the 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 put the file name in the cookie
$ Filename = date ('ymdhis ');
Setcookie ('fileid', $ filename, time () + 60*60*24 );

// 3. open the file for reading and writing
$ Globals ['fp'] = fopen ($ filename, 'W + ');

} // End of if-else

// 4. store the data in the file to the ultra-Global Array $ _ session
While (! Feof ($ globals ['fp']) {
// Read a row from the file
$ Buffer = fgets ($ globals ['fp']);
// Process the read row
$ Tmparr = explode ('=', trim ($ buffer, 'rn '));

// Add it to the session Array
If (count ($ tmparr) = 2 ){
$ Globals ['_ session'] [$ tmparr [0] = $ tmparr [1];
}
} // End of while

} // End of session_file_start ()

// Register the function of the session variable
Function session_file_register ($ key, $ val ){

// Set the 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 the 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

The Code is as follows:

<? Php
// Determine the encoding format
Header ('content-type: text/html; charset = UTF-8 ');
Include ("session-file.php ");
// Test the function:
// Enable session
Session_file_start ();
// Register session Variables
$ Key = 'username ';
$ Val = 'lsl ';
Session_file_register ($ key, $ val );
Session_file_register ('username', 'lisa ');
// Print the session Array
Echo $ _ session ['username'];
?>
Next Page


Test File: 2.php

The Code is as follows:

<? Php
// Determine the encoding format
Header ('content-type: text/html; charset = UTF-8 ');
Include ("session-file.php ");
// Test the function:
// Enable session
Session_file_start ();
Echo $ _ session ['username'];
?>

I hope this article will help you with PHP programming.

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.