Php custom File Save session implementation-PHP source code

Source: Internet
Author: User
Ec (2); session. inc. php tutorial file: defines the file storage of sessions. 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. As mentioned above, session is not a simple concept of time. A session also includes specific users and servers. Therefore, in more detail, the scope of global variables defined in a session refers to script ec (2) and script

// Session. inc. php tutorial file: defines the file storage of sessions. 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. As mentioned above, session is not a simple concept of time. A session also includes specific users and servers. Therefore, in more detail, the scope of global variables defined in 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
This topic describes how to create a session. Very simple, really. Start the 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. Php automatically creates the session file. After executing this program, we can find the session file in the temporary folder of the system. The file name is generally sess_4c83638b3b0dbf65583181c2f89168ec, followed by a 32-bit encoded random string. Open it in the editor and check its content: admin | n;


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

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

// 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'];


?>

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.