php3增加session的功能 |
|
jackkcg
站務副站長 發表:891 回覆:1050 積分:848 註冊:2002-03-23 發送簡訊給我 |
php3中沒有session的功能,以下是一個php4的backport,能?php3提供session file: sessions.php3
-----------------------------------------------------
<?
/*********************************************************************
********
*
*
* Web Application Development with PHP
*
* by
*
* Tobias Ratschiller and Till Gerken
*
*
*
* Copyright (c) 2000, New Riders Publishing
*
*
*
*********************************************************************
********
*
*
* $Title: PHP 3 implementation of PHP 4's session management API $
*
* $Chapter: Web Application Concepts $
*
* $Executable: false $
*
*
*
* $Description:
*
* This is a backport of the PHP 4 session_* functions to native PHP -
so *
* that you can use the same session management functions under both
*
* versions of PHP. They're believed to be about 75% compatible at the
*
* moment, but it's already possible to use the most common stuff. $
*
*
*
*********************************************************************
********/ /*
* Differences from PHP 4:
* - no URL rewriting (of course)
* - options aren't specified in the php.ini but in the session c
lass below
* - auto_start doesn't work with user callbacks
* - the session ID is produced by a different algorithm
* - shared memory support is still missing
* - <?=SID?> doesn't work - use <?print($SID);?>
* - the WDDX serializer doesn't work yet.
* - serializing objects is limited due to PHP 3's serializer()
*
* Notes:
* The session class contains the configuration variables. Th
is is the
* only part of the code you should need to edit.
*
* To reproduce the module concept used in PHP 4's session li
brary, we
* use classes. An example class has been been provided: file
s. You can
* easily create your own classes, for example a class mysql
to store
* session data to MySQL. It needs to provide the following f
unctions:
* bool open(string save_path, string sess_name):
* used on startup of a session to initialize variables o
r memory
* returns false on error or true on success
* bool close:
* used on shutdown of a session to unset variables or fr
ee memory
* mixed read(string sess_id):
* reads the session data of the session identified with
sess_id.
* returns false on error or the serialized session data * bool write(string sess_id, string val):
* saves the session data of the session identified with
sess_id
* returns false on error or true on success
* bool destroy(string sess_id):
* destroy the session identified with sess_id
* returns false on error or true on success
* bool gc(int max_lifetime):
* provides garbage collection to remove sessions older t
han
* time() - max_lifetime
* returns false on error or true on success
*
* While it may be faster to provide your own class, the reco
mmended way
* to add storage modules is to use session_set_save_handler(
), as this
* is compatible to PHP 4.
*/ $SID = "";
class session
{
// Public variables
var $auto_start = true;
var $save_path = "/tmp";
var $name = "PHPSESSID";
var $save_handler = "files";
var $lifetime = 0;
var $gc_probability = 1;
var $gc_maxlifetime = 0;
var $serialize_handler = "php";
var $extern_referer_check = false;
var $use_cookies = true;
var $ID; // Private variables
var $nr_open_sessions = 0;
var $mod_name = "";
var $id;
var $delimiter = "\n";
var $delimiter_value = "[==]"; function session()
{
$this->mod_name = $this->save_handler;
}
} class user
{
var $open_func;
var $close_func;
var $read_func;
var $write_func;
var $destroy_func;
var $gc_func; function open($save_path, $sess_name)
{
$func = $this->open_func;
if(function_exists($func))
{
return($func($save_path, $sess_name));
} return(true);
} function close($save_path, $sess_name)
{
$func = $this->close_func;
if(function_exists($func))
{
return($func());
} return(true);
} function read($sess_id)
{
$func = $this->read_func; return($func($sess_id));
} function write($sess_id, $val)
{
$func = $this->write_func; return($func($sess_id, $val));
} function destroy($sess_id)
{
$func = $this->destroy_func;
if(function_exists($func))
{
return($func($sess_id));
} return(true);
} function gc($max_lifteime)
{
$func = $this->gc_func;
if(function_exists($func))
{
return($func($max_lifetime));
} return(true);
} } class files
{
function open($save_path, $sess_name)
{
return(true);
} function close()
{
return(true);
} function read($sess_id)
{
global $session; // Open, read in, close file with session data
$file = $session->save_path."/sess$sess_id";
if (!file_exists($file))
{
// Create it
touch($file);
}
$fp = fopen($file, "r") or die("Could not open session file ($
file).");
$val = fread($fp, filesize($file));
fclose($fp); return($val);
} function write($sess_id, $val)
{
global $session; // Open, write to, close file with session data
$file = $session->save_path."/sess$sess_id";
$fp = fopen($file, "w") or die("Could not write session file (
$file)");
$val = fputs($fp, $val);
fclose($fp); return(true);
} function destroy($sess_id)
{
global $session; $file = $session->save_path."/sess$sess_id";
unlink($file); return(true);
} function gc($max_lifetime)
{
// We return true, since all cleanup should be handled by
// an external entity (i.e. find -ctime x | xargs rm)
return(true);
}
} function _session_create_id()
{
return(md5(uniqid(microtime())));
} function _php_encode()
{
global $session; $ret = "";
// Create a string containing the serialized variables
for ($i=0; $i
------
********************************************************** 哈哈&兵燹 最會的2大絕招 這個不會與那個也不會 哈哈哈 粉好 Delphi K.Top的K.Top分兩個字解釋Top代表尖端的意思,希望本討論區能提供Delphi的尖端新知 K.表Knowlege 知識,就是本站的標語:Open our mind |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |