2011-03-09 96 views
2

我一直在试图让我的会话在我的子域中运行,我很确定我在星期一工作,但星期二添加了一些代码后它不工作星期三!我已使用代码ini_set("session.cookie_domain", $domain);,其中$domain = .example.com会话和子域名

我的网站的主页当前位于test.example.com,我通过test.example.com/login访问登录页面。当我输入此地址时,地址栏中的网址会自动更改为http://www.test.example.com/login,这就是问题所在。会话创建为www.test.example.com,但该网站上的大多数链接指向test.example.com/<sub folder>

我能想到的唯一可能就是抛弃它是我处理会话的方式。在每个页面中,一个会话开始。首先设置ini_set("session.cookie_domain", $domain);,然后会话开始。接下来我检查会话是否已过期。如果会话已过期,则当前会话被销毁并取消设置,则会创建一个新会话。剩下的就是设置用户信息。

我最近添加的唯一东西是会话过期检查器。我试过绕过它,但它没有改变任何东西。

任何帮助是极大的赞赏。如果它使得它更容易,我可以发布代码。

Mike

回答

1

请添加一些代码:)。

我只能告诉你我们是如何实现同样的功能的。尝试添加

<directory "/path/to/your/docroot"> 
    php_value session.cookie_domain ".example.com" 
</directory> 

到您的虚拟主机配置。这是我们为了使此功能正常工作而必须做的唯一事情。现在,我们可以使用相同的Cookie访问所有子域,而无需添加所有额外的代码。我不认为这是一个解决方案,但是这种方法使得测试更加简单。

编辑

您可以在Web服务器的配置设置虚拟主机。假设你使用apache,它们将会在httpd.conf中,或者存在于你的httpd.conf中包含的文件系统上的其他文件中。 httpd.conf位于你的系统上取决于你的配置,但是如果你使用Linux,它可能会在/ etc/apache,/ etc/httpd,/ usr/local/apache,/ usr/local/httpd中的某个地方

一旦找到这个文件将有一个或多个条目是这样的:

<VirtualHost *:80> 
    ServerAdmin [email protected] 
    DocumentRoot /var/www/yourdomain/www 
    ServerName yourdomain.org 
    <directory "/var/www/yourdomain/www"> 
       Options FollowSymLinks Includes 
       AllowOverride All 
       Order allow,deny 
       Allow from all 
     </directory> 
</VirtualHost> 

并修改它看起来像这样的代码:

<VirtualHost *:80> 
    ServerAdmin [email protected] 
    DocumentRoot /var/www/yourdomain/www 
    ServerName yourdomain.org 
    <directory "/var/www/yourdomain/www"> 
       Options FollowSymLinks Includes 
       AllowOverride All 
       Order allow,deny 
       Allow from all 
       php_value session.cookie_domain ".yourdomain.org" 
     </directory> 
</VirtualHost> 

通知的php_value session.cookie_domain ".yourdomain.org"线。

将此行添加到此域的所有服务器配置中,您的Cookie将被共享。

+0

我无法将此代码添加到此回复消息中,因此我为此主题创建了一个新答案,希望代码易于遵循。 – michaeln31 2011-03-09 18:35:30

+0

我无法访问我的服务器的那部分,这就是为什么我一直在尝试使用ini_set。我将与我的供应商联系并询问他们。 ini_set应该不工作? – michaeln31 2011-03-12 15:58:23

0

如果不知道更多详细信息,则无法进行调试。

您可能需要首先检查cookie是否设置正确,以及它们是否实际返回到服务器。

使用一种工具,可以让您在浏览器上查看标题(用于Firefox的web开发工具栏/ liveheaders/firebug),然后查看服务器是否实际请求浏览器接受cookie - 以及内容。

+0

当我登录时,我被重定向回主页面test.example.com。本页面没有与会话交互,但是如果我将网址更改为www.test.example.com,会话将正常工作。 – michaeln31 2011-03-09 18:33:35

0

原谅我不知道,但'虚拟主机配置'是什么。我的代码的运行是这样的:

主要页面将包括session.php

function Session() 
{ 
    $this->time = time(); 
    $this->startSession(); 
} 

function startSession() 
{ 
    global $serverFunctions; 

    $serverFunctions->setSubdomainSharing(); 

    session_start(); 

    $this->checkSessionLife(); 

    //check if user is logged in 
    $this->logged_in = $this->checkLogin(); 

    //if user is not logged in then it is given guest credintials 
    if (!$this->logged_in) 
    { 
     $this->user_name = $_SESSION['user_name'] = GUEST_NAME; 
     $this->user_level = $_SESSION['user_level'] = GUEST_LEVEL; 
    } 
    if (!isset($_SESSION['language'])) 
    { 
     $this->setLanguage("translation_english"); 
    } 
    else 
    { 
     $this->user_language = $_SESSION['language']; 
    } 
} 

function checkSessionLife() 
{ 
    global $serverFunctions; 

    if (isset($_SESSION['start_time'])) 
    { 
     $session_life = time() - $_SESSION['start_time']; 

     if ($session_life > 15) 
     { 
      $this->logout(); 
      $serverFunctions->setSubdomainSharing(); 
      session_start(); 
     } 
    } 
    else if (!isset($_SESSION['start_time'])) 
    { 
     //logout any session that was created 
     //before expiry was implemented 
     $this->logout(); 
     $serverFunctions->setSubdomainSharing(); 
     session_start(); 
    } 

    $_SESSION['start_time'] = time(); 
} 

function logout() 
{ 
    global $database; 

    // Unset session variables 
    session_destroy(); 
    session_unset(); 
    //session_regenerate_id(true); 


    $this->logged_in = false; 

    // Set user level to guest 
    $this->user_name = GUEST_NAME; 
    $this->user_level = GUEST_LEVEL; 
} 

会话文件包括一个名为serverFunctions另一个PHP文件。这只是一个允许我格式化URL等的类。

function getAddressPrefix() 
{ 
    $address_prefix = ""; 

    if ($_SERVER['SERVER_ADDR'] == '127.0.0.1') 
    { 
     $address_prefix = "http://localhost/myproject"; 
    } 
    else 
    { 
     $address_prefix = $this->getServerName(); 
    } 

    return $address_prefix; 
} 

function getServerName() 
{ 
    return "http://" . str_replace("www.", "", $_SERVER['SERVER_NAME']); 
} 

function formatRequestingPage() 
{ 
    return $this->getServerName() . $_SERVER['SCRIPT_NAME']; 
} 

function setSubdomainSharing() 
{ 

    if ($_SERVER['SERVER_ADDR'] != '127.0.0.1') 
    { 
     $domain = $this->getServerName(); 

     do 
     { 
      $domain = substr($domain, strpos($domain, ".", 0) + 1); 
     } 
     while (substr_count($domain, ".") > 1); 
     $domain = ".".$domain; 

     ini_set("session.cookie_domain", $domain); 
    } 
} 

当用户登录时,登录请求被process_request.php

function LoginReq() 
{ 
    global $session; 
    global $variables; 
    global $serverFunctions; 

    $retval = $session->login($_POST['user_name'], $_POST['password']); 

    if ($retval) 
    { 
     header("Location: " . $serverFunctions->getAddressPrefix()); 
     exit(); 
    } 
    else 
    { 
     $_SESSION['variables_array'] = $_POST; 
     $_SESSION['error_array'] = $variables->getErrorArray(); 
     header("Location: " . $serverFunctions->getAddressPrefix() . "/login/"); 
     exit(); 
    } 
} 

如果我错过什么或需要解释发生的事情多一点让我知道处理。