分别配置每个站点:
<?php
$cfgsession['file'] = "../sessions_global.txt";
$cfgsession['keepalive'] = 7200;
?>
要让多个站点共享会话,让它们使用相同的$cfgsession['file']
. 将来自一个站点的会话包含在对另一个域的请求中(可能是 Jack 推荐的),并且只要您没有发现他们在另一个浏览器或其他任何方式中发出请求(请采取措施阻止会话劫持),让他们使用 $_GET 指定会话。例如:
include ("../session.php");
if (isset($_COOKIE['session'])) session_begin($_COOKIE['session'], $_SERVER['HTTP_USER_AGENT'] . "+" . $_SERVER['HTTP_ACCEPT_CHARSET'], $_SERVER['REMOTE_ADDR']);
else session_begin("", $_SERVER['HTTP_USER_AGENT'] . "+" . $_SERVER['HTTP_ACCEPT_CHARSET'], $_SERVER['REMOTE_ADDR']);
setcookie("session", session_identity(), 0);
然后只需滚动您自己的 session_ 函数:
<?php
function session_begin($mysession = "", $key = "", $client = "") {
global $cfgsession;
if (!preg_match("/^[a-z0-9]{32}$/i", $mysession)) $mysession = md5(microtime());
$error = false;
$client = trim($client);
$key = trim($key);
$cfgsession['returning'] = false;
if ($chandle = @tmpfile()) {
if ($shandle = @fopen($cfgsession['file'], "rb")) {
flock($shandle, LOCK_SH);
fputs($chandle, $mysession . " " . time() . " $" . $client . " $" . $key . "\n");
while (!feof($shandle)) {
$sline = explode(" ", trim(fgets($shandle)), 4);
if ($sline[1] >= (time() - $cfgsession['keepalive'])) {
if (($sline[0] == $mysession) && ($sline[3] == "$" . $key)) {
$cfgsession['client'] = substr($sline[2], 1);
$cfgsession['returning'] = true;
} elseif (count($sline) > 2) fputs($chandle, implode(" ", $sline) . "\n");
}
}
fclose($shandle);
fseek($chandle, 0);
if ($shandle = @fopen($cfgsession['file'], "cb")) {
if (flock($shandle, LOCK_EX)) {
ftruncate($shandle, 0);
$cfgsession['count'] = 0;
while (!feof($chandle)) {
$cline = trim(fgets($chandle));
fputs($shandle, $cline . "\n");
$cfgsession['count']++;
}
} else $error = true;
fclose($shandle);
} else $error = true;
} else $error = true;
fclose($chandle);
} else $error = true;
if (($cfgsession['returning'] == false) && ($mysession == $cfgsession['session'])) {
$cfgsession['returning'] = true;
$mysession = md5(microtime());
}
$cfgsession['session'] = $mysession;
if ($error) return -1;
else return 0;
}
function session_count() {
global $cfgsession;
return $cfgsession['count'];
}
function session_client() {
global $cfgsession;
return $cfgsession['client'];
}
function session_id() {
global $cfgsession;
return $cfgsession['session'];
}
function session_index() {
global $cfgsession;
$index_return = array();
if ($uhandle = @fopen($cfgsession['file'], "rb")) {
flock($uhandle, LOCK_SH);
while (!feof($uhandle)) {
$uline = explode(" ", trim(fgets($uhandle)), 4);
foreach ($uline as &$value) {
if ($value[0] == "$") $value = substr($value, 1);
}
if (count($uline) >= 2) $index_return[] = $uline;
}
fclose($uhandle);
}
return $index_return;
}
function session_returning() {
global $cfgsession;
return $cfgsession['returning'];
}
?>