Several supplementary functions of the session (iii) _php tutorial

Source: Internet
Author: User
!! Note: Before using this function, you must first configure the php.ini file, Session.save_hadler=user, otherwise, Session_set_save_handler () will not take effect.
In addition, according to my test, if you want to make such a session cross-page use, but also in each session of the script file to add your custom function and Session_set_save_handler, so, the best way is to create a separate file, Include in each script that uses the session.
The following example provides a basic session save method, similar to the default files method.
This is also easy to do if you want to use a database to implement it.
Example 1. Session_set_save_handler () example
$#@60;? Php
function open ($save _path, $session _name) {
Global $sess _save_path, $sess _session_name;
$sess _save_path = $save _path;
$sess _session_name = $session _name;
return (true);
}
function Close () {
return (true);
}
function Read ($id) {
Global $sess _save_path, $sess _session_name;
$sess _file = "$sess _save_path/sess_$id";
if ($fp = @fopen ($sess _file, "R")) {
$sess _data = fread ($fp, FileSize ($sess _file));
Return ($sess _data);
} else {
Return ("");
}
}
function Write ($id, $sess _data) {

Global $sess _save_path, $sess _session_name;
$sess _file = "$sess _save_path/sess_$id";
if ($fp = @fopen ($sess _file, "W")) {
Return (Fwrite ($fp, $sess _data));
} else {
return (false);
}
}
function Destroy ($id) {
Global $sess _save_path, $sess _session_name;
$sess _file = "$sess _save_path/sess_$id";
Return (@unlink ($sess _file));
}

http://www.bkjia.com/PHPjc/532386.html www.bkjia.com true http://www.bkjia.com/PHPjc/532386.html techarticle !! Note: Before using this function, you must first configure the php.ini file, Session.save_hadler=user, otherwise, Session_set_save_handler () will not take effect. In addition, according to my test, if you want to ...

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