Home  >  Article  >  Backend Development  >  How to save session in php custom file, _PHP tutorial

How to save session in php custom file, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:12:03853browse

How to save session in php custom file,

The example in this article describes the method of saving session in PHP custom file. Share it with everyone for your reference. The specific implementation method is as follows:

session.inc.php file: defines the file storage of session, and the session solution is to provide a method to define global variables in PHP scripts, so that this global variable is valid for all PHP scripts in the same session. We all know that session is not a simple time concept. A session also includes specific users and servers. Therefore, in more detail, the scope of global variables defined in a session refers to the user corresponding to the session. All PHP accessed, for example, user a defines a global variable $user="wind" through session, and user b defines a global variable $user="jane" through session, then in the PHP script accessed by user a, The value of $user is wind.

How to create session in php

Let’s start with how to create a session. It’s very simple. 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 wants to call the session variable, you must start it before calling the session, use the session_start() function, you do not need to set anything else, php automatically completes the creation of the session file, After executing this program, we can find the session file in the system temporary folder. Generally, the file name is in the form: sess_4c83638b3b0dbf65583181c2f89168ec, followed by a 32-bit encoded random string. Open it with an editor and take a look at its content: admin |n;, PHP example code is as follows:

Copy code The code is as follows:
//Define a super global array
$_session = array();
//Define file handle
$fp = null;

//User-defined opening 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 into the super global array $_session
while (!feof($globals['fp'])) {
//Read a line from the file
$buffer = fgets($globals['fp']);
//Process the line read
$tmparr = explode('=', trim($buffer, 'rn'));

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

} //end of session_file_start()

//Function to register session variables
function session_file_register($key, $val) {

//Set session variables
$globals['_session'][$key] = $val;

//Put this variable into 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 empty
$globals['_session'] = array();
} //end of session_file_destroy()

Test code file: 1.php
Copy code The code is as follows:
//Determine the encoding format
header('content-type: text/html; charset=utf-8');
include("session-file.php");
//Test function:
//Open session
session_file_start();
//Register 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 code The code is as follows:
//Determine 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 be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/925130.htmlTechArticleHow to save session in PHP custom file. This article describes the method of saving session in PHP custom file. Share it with everyone for your reference. The specific implementation method is as follows: session.inc.php file...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn