使会话变量在动态创建的子域上处于活动状态

时间:2013-01-20 12:12:26

标签: php database

我正在开发一个允许用户动态创建自己的子域的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>
     &nbsp;&nbsp; | &nbsp;&nbsp; 
     <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;

&GT;

注意:所有子域都只使用一段代码进行管理,其中与该特定子域相关的数据将根据子域动态地从数据库转储。

介于共享主机服务和使用* .streamicon.com作为根域之间。使用* .streamicon.com作为我的根域,因为它允许我动态创建'n'个子域。

0 个答案:

没有答案