Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP Manual > session_set_save_handler - Sets user-level session storage functions





ลองใช้ค้นหาข้อมูล


   
Bookmark.

session_set_save_handler

(PHP 4, PHP 5)

session_set_save_handlerSets user-level session storage functions

Description

bool session_set_save_handler ( callback $open , callback $close , callback $read , callback $write , callback $destroy , callback $gc )

session_set_save_handler() sets the user-level session storage functions which are used for storing and retrieving data associated with a session. This is most useful when a storage method other than those supplied by PHP sessions is preferred. i.e. Storing the session data in a local database.

Parameters

open

Open function, this works like a constructor in classes and is executed when the session is being opened. The open function expects two parameters, where the first is the save path and the second is the session name.

close

Close function, this works like a destructor in classes and is executed when the session operation is done.

read

Read function must return string value always to make save handler work as expected. Return empty string if there is no data to read. Return values from other handlers are converted to boolean expression. TRUE for success, FALSE for failure.

write

Note: The "write" handler is not executed until after the output stream is closed. Thus, output from debugging statements in the "write" handler will never be seen in the browser. If debugging output is necessary, it is suggested that the debug output be written to a file instead.

destroy

The destroy handler, this is executed when a session is destroyed with session_destroy() and takes the session id as its only parameter.

gc

The garbage collector, this is executed when the session garbage collector is executed and takes the max session lifetime as its only parameter.

Return Values

Returns TRUE on success or FALSE on failure.

Examples

Example #1 session_set_save_handler() example

The following example provides file based session storage similar to the PHP sessions default save handler files . This example could easily be extended to cover database storage using your favorite PHP supported database engine.

<?php
function open($save_path$session_name)
{
  global 
$sess_save_path;

  
$sess_save_path $save_path;
  return(
true);
}

function 
close()
{
  return(
true);
}

function 
read($id)
{
  global 
$sess_save_path;

  
$sess_file "$sess_save_path/sess_$id";
  return (string) @
file_get_contents($sess_file);
}

function 
write($id$sess_data)
{
  global 
$sess_save_path;

  
$sess_file "$sess_save_path/sess_$id";
  if (
$fp = @fopen($sess_file"w")) {
    
$return fwrite($fp$sess_data);
    
fclose($fp);
    return 
$return;
  } else {
    return(
false);
  }

}

function 
destroy($id)
{
  global 
$sess_save_path;

  
$sess_file "$sess_save_path/sess_$id";
  return(@
unlink($sess_file));
}

function 
gc($maxlifetime)
{
  global 
$sess_save_path;

  foreach (
glob("$sess_save_path/sess_*") as $filename) {
    if (
filemtime($filename) + $maxlifetime time()) {
      @
unlink($filename);
    }
  }
  return 
true;
}

session_set_save_handler("open""close""read""write""destroy""gc");

session_start();

// proceed to use sessions normally

?>

Notes

Warning

As of PHP 5.0.5 the write and close handlers are called after object destruction and therefore cannot use objects or throw exceptions. The object destructors can however use sessions.

It is possible to call session_write_close() from the destructor to solve this chicken and egg problem.

Warning

Current working directory is changed with some SAPIs if session is closed in the script termination. It is possible to close the session earlier with session_write_close().

See Also



ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่