我正在开发一个允许用户动态创建自己的子域的 Web 项目。在创建子域之前,他们应该登录到网站。现在想知道如何在他访问的子域上设置活跃用户的会话变量。尝试了很多功能,如 session_set_cookie_params(0, '/', '.example.com'); **ini_set('session.cookie_domain', '.example.com');**
但一切都没有。没有功能起作用。所以请建议我如何处理这个问题。
这是我的代码,一旦用户登录就开始会话: checkusrlog.php
<?php
//for session to be active on subdomain
session_set_cookie_params(0, '/', '.xyz.com');
session_start(); // Start Session First Thing
error_reporting(E_ALL);
ini_set('display_errors', '1');
include_once "connectiontomysql.php"; // Connect to the database
$dyn_www = $_SERVER['HTTP_HOST'];
//------ CHECK IF THE USER IS LOGGED IN OR NOT AND GIVE APPROPRIATE OUTPUT -------
$logOptions = ''; // Initialize the logOptions variable that gets printed to the page
$newMessage = '';
// 如果没有设置会话变量和 cookie 变量,此代码运行
if (!isset($_SESSION['idx'])) {
if (!isset($_COOKIE['idCookie'])) {
$logOptions = '<a href="http://' . $dyn_www . '/index.php">Register Account</a>
|
<a href="http://' . $dyn_www . '/index.php">Log In</a>';
}
}
// 如果为没有 cookie 的登录用户设置了会话 ID,请记住我的功能集
if (isset($_SESSION['idx'])) {
$decryptedID = base64_decode($_SESSION['idx']);
$id_array = explode("p3h9xfn8sq03hs2234", $decryptedID);
$logOptions_id = $id_array[1];
} else if (isset($_COOKIE['idCookie'])) {// If id cookie is set, but no session ID is set yet, we set it below and update stuff
$decryptedID = base64_decode($_COOKIE['idCookie']);
$id_array = explode("nm2c0c4y3dn3727553", $decryptedID);
$userID = $id_array[1];
$userPass = $_COOKIE['passCookie'];
// Get their user first name to set into session var
$sql_uname = mysql_query("SELECT username, email FROM siteMembers WHERE id='$userID' AND password='$userPass' LIMIT 1");
$numRows = mysql_num_rows($sql_uname);
if ($numRows == 0) {
// Kill their cookies and send them back to homepage if they have cookie set but are not a member any longer
setcookie("idCookie", '', time()-42000, '/');
setcookie("passCookie", '', time()-42000, '/');
header("location: index.php"); // << makes the script send them to any page we set
exit();
}
while($row = mysql_fetch_array($sql_uname)){
$username = $row["username"];
$useremail = $row["email"];
}
$_SESSION['id'] = $userID; // now add the value we need to the session variable
$_SESSION['idx'] = base64_encode("g4p3h9xfn8sq03hs2234$userID");
$_SESSION['username'] = $username;
$_SESSION['useremail'] = $useremail;
$_SESSION['userpass'] = $userPass;
$logOptions_id = $userID;
?>
注意:所有子域仅使用单个代码进行管理,其中属于该特定子域的数据是基于子域从数据库动态转储的。
在这两者之间我正在开发共享托管服务并使用 *.streamicon.com 作为根域。使用 *.streamicon.com 作为我的根域,因为它允许我动态创建“n”个子域。