📄 session.inc.php
字号:
<?
/*apr-04-2004 []pythoning [] signup at www.f2s.ph, postgresql/sqlite is free [] to make the session good, i will be zeroing some values to always delete the session [] mala livehelp na configuration file, writeable config file. [] convert editing of catalog into plane textarea jst like in python */
function SessionSetup()
{ /** * Set up session handling * * Set all PHP options for PostNuke session handling */ global $HTTP_SERVER_VARS, $HTTP_HOST;
$path = Polerio::GetBaseURI();
if (empty($path)) {
$path = '/';
}
// PHP configuration variables
// Stop adding SID to URLs
ini_set('session.use_trans_sid', 0);
// User-defined save handler
ini_set('session.save_handler', 'user');
// How to store data
ini_set('session.serialize_handler', 'php');
// Use cookie to store the session ID
ini_set('session.use_cookies', 1);
// Name of our cookie
ini_set('session.name', 'POLERIOSID');
// Lifetime of our cookie
$seclevel = "High";
switch ($seclevel) {
case 'High':
// Session lasts duration of browser
$lifetime = 0;
break;
case 'Medium':
// Session lasts set number of 5 days
$lifetime = 5 * 86400;
break;
case 'Low':
// Session lasts unlimited number of days (well, lots, anyway)
// (Currently set to 25 years)
$lifetime = 788940000;
break;
}
ini_set('session.cookie_lifetime', $lifetime);
$IsIntranet = false;
if($IsIntranet) {
// Cookie path
ini_set('session.cookie_path', $path);
// Cookie domain
$domain = $HTTP_SERVER_VARS['HTTP_HOST'];
if (empty($domain)) {
$domain = $HTTP_HOST;
}
$domain = preg_replace('/:.*/', '', $domain);
ini_set('session.cookie_domain', $domain);
// Referer check
ini_set('session.referer_check', "$domain$path");
}
// Garbage collection
ini_set('session.gc_probability', 1);
// Inactivity timeout for user sessions
$secinactivemins = 0;
ini_set('session.gc_maxlifetime', $secinactivemins * 60);
// Auto-start session
ini_set('session.auto_start', 1);
// Session handlers
session_set_save_handler("SessionOpen",
"SessionClose",
"SessionRead",
"SessionWrite",
"SessionDestroy",
"SessionGC");
return true;
}
function SessionInit() { /** * Initialise session */ global $HTTP_SERVER_VARS; list($pmldbconn) = Polerio::DBGetConn(); $table = Polerio::DBGetTables(); // First thing we do is ensure that there is no attempted pollution // of the session namespace foreach($GLOBALS as $k=>$v) { if (preg_match('/^PMLV/', $k)) { return false; } } // Kick it session_start(); $sessid = session_id(); // Get (actual) client IP addr if (!empty($HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR'])) { $ipaddr = preg_replace('/,.*/', '', $HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR']); } else { $ipaddr = $HTTP_SERVER_VARS['REMOTE_ADDR']; } $sessioninfocolumn = &$table['session_info_column']; $sessioninfotable = $table['session_info']; $query = "SELECT $sessioninfocolumn[ipaddr] FROM $sessioninfotable WHERE $sessioninfocolumn[sessid] = '" . Polerio::VarPrepForStore($sessid) . "'"; $result = $pmldbconn->Execute($query); if($pmldbconn->ErrorNo() != 0) { return false; } if (!$result->EOF) { $result->Close(); SessionCurrent($sessid); } else { SessionNew($sessid, $ipaddr); // Generate a random number, used for // some authentication srand((double)microtime()*1000000); SessionSetVar('rand', rand()); } return true; }function SessionGetVar($name)
{ /* * Session variables here are a bit 'different'. Because they sit in the * global namespace we use a couple of helper functions to give them their * own prefix, and also to force users to set new values for them if they * require. This avoids blatant or accidental over-writing of session * variables. * /** * Get a session variable * * @param name name of the session variable to get */
global $HTTP_SESSION_VARS;
$var = "PMLV$name";
global $$var;
if (!empty($$var)) {
return $$var;
}
return;
}
function SessionSetVar($name, $value)
{
/** * Set a session variable * @param name name of the session variable to set * @param value value to set the named session variable */ $var = "PMLV$name";
global $$var;
$$var = $value;
session_register($var);
return true;
}
function SessionDelVar($name)
{
/** * Delete a session variable * @param name name of the session variable to delete */ $var = "PMLV$name";
global $$var;
unset($$var);
session_unregister($var);
return true;
}
function SessionCurrent($sessid)
{
/** * Continue a current session * @private * @param sessid the session ID */ list($pmldbconn) = Polerio::DBGetConn();
$table = Polerio::DBGetTables();
$sessioninfocolumn = &$table['session_info_column'];
$sessioninfotable = $table['session_info'];
// Touch the last used time
$query = "UPDATE $sessioninfotable
SET $sessioninfocolumn[lastused] = " . time() . "
WHERE $sessioninfocolumn[sessid] = '" . Polerio::VarPrepForStore($sessid) . "'";
$result = $pmldbconn->Execute($query);
if ($pmldbconn->ErrorNo() != 0) {
return false;
}
return true;
}
function SessionNew($sessid, $ipaddr)
{
/** * Create a new session * @private * @param sessid the session ID * @param ipaddr the IP address of the host with this session */ list($pmldbconn) = Polerio::DBGetConn();
$table = Polerio::DBGetTables();
$sessioninfocolumn = &$table['session_info_column'];
$sessioninfotable = $table['session_info'];
$query = "INSERT INTO $sessioninfotable (
$sessioninfocolumn[sessid],
$sessioninfocolumn[ipaddr],
$sessioninfocolumn[uid],
$sessioninfocolumn[firstused],
$sessioninfocolumn[lastused])
VALUES
('" . Polerio::VarPrepForStore($sessid) . "',
'" . Polerio::VarPrepForStore($ipaddr) . "',
0,
" . time() . ",
" . time() . ")";
$pmldbconn->Execute($query); if ($pmldbconn->ErrorNo() != 0) {
return false;
}
return true;
}
function SessionOpen($path, $name)
{
/** * PHP function to open the session * @private */ // Nothing to do - database opened elsewhere
return true;
}
function pnSessionClose()
{
/** * PHP function to close the session * @private */ // Nothing to do - database closed elsewhere
return true;
}
function SessionRead($sessid)
{
/** * PHP function to read a set of session variables * @private */
list($pmldbconn) = Polerio::DBGetConn();
$table = Polerio::DBGetTables();
$sessioninfocolumn = &$table['session_info_column'];
$sessioninfotable = $table['session_info'];
$query = "SELECT $sessioninfocolumn[vars]
FROM $sessioninfotable
WHERE $sessioninfocolumn[sessid] = '" . Polerio::VarPrepForStore($sessid) . "';";
$result = $pmldbconn->Execute($query);
if ($pmldbconn->ErrorNo() != 0) {
return false;
}
if (!$result->EOF) {
list($value) = $result->fields;
} else {
$value = '';
}
$result->Close();
return($value);
}
function SessionWrite($sessid, $vars)
{
/** * PHP function to write a set of session variables * @private */ list($pmldbconn) = Polerio::DBGetConn();
$table = Polerio::DBGetTables();
$sessioninfocolumn = &$table['session_info_column'];
$sessioninfotable = $table['session_info'];
$query = "UPDATE $sessioninfotable
SET $sessioninfocolumn[vars] = '" . Polerio::VarPrepForStore($vars) . "'
WHERE $sessioninfocolumn[sessid] = '" . Polerio::VarPrepForStore($sessid) . "'";
$pmldbconn->Execute($query);
if ($pmldbconn->ErrorNo() != 0) {
return false;
}
return true;
}
function SessionDestroy($sessid)
{
/** * PHP function to destroy a session * @private */ list($pmldbconn) = Polerio::DBGetConn();
$table = Polerio::DBGetTables();
$sessioninfocolumn = &$table['session_info_column'];
$sessioninfotable = $table['session_info'];
$query = "DELETE FROM $sessioninfotable
WHERE $sessioninfocolumn[sessid] = '" . Polerio::VarPrepForStore($sessid) . "'";
$pmldbconn->Execute($query);
if ($pmldbconn->ErrorNo() != 0) {
return false;
}
return true;
}
function SessionGC($maxlifetime)
{
/** * PHP function to garbage collect session information * @private */ list($pmldbconn) = Polerio::DBGetConn();
$table = Polerio::DBGetTables();
$sessioninfocolumn = &$table['session_info_column'];
$sessioninfotable = $table['session_info'];
$secinactivemins = 0;
$where = "WHERE $sessioninfocolumn[lastused] < " . (time() - (0));
$query = "DELETE FROM $sessioninfotable $where";
$pmldbconn->Execute($query);
if ($pmldbconn->ErrorNo() != 0) {
return false;
}
return true;
}
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -