Sessions and subdomains

☆樱花仙子☆ 提交于 2019-12-06 05:15:08

Please add some code :).

I can only tell you how we achieved the same functionality. Try adding

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

to your virtual host configs. This was the only thing we had to do to make this functionality work. Now we can access all subdomains with the same cookies without adding all the extra code. I don't say this is a solutions, but this approach makes testing a lot less complicated.

Edit

You can set virtual hosts in the configuration of your webserver. Assuming you use apache they will be either in httpd.conf or are present in other files on the filesystem which are included in your httpd.conf. Where httpd.conf is located on your system depends on your configuration, but if you use Linux it will probably be somewhere in /etc/apache, /etc/httpd, /usr/local/apache, /usr/local/httpd

Once you have located this file it will have one or more entries like this:

<VirtualHost *:80>
    ServerAdmin webmaster@yourdomain.org
    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>

And modify the code that it looks like this:

<VirtualHost *:80>
    ServerAdmin webmaster@yourdomain.org
    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>

Notice the php_value session.cookie_domain ".yourdomain.org" line.

Add this line to all server configuration for this domain and your cookies will be shared.

This is impossible to debug without knowing more details.

You might want to first check if the cookies are being set properly, and if they are actually being returned to the server.

Use a tool which lets you see headers on your browser (webdeveloper toolbar / liveheaders / firebug for Firefox) and see if the server is actually requesting that the browser accept a cookie - and for what.

michaeln31

Forgive me for not knowing but what 'virtual host configs' is. My code runs something like this:

The main page will include 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;
}

The session file includes another PHP file called serverFunctions. This is just a class that allows me to format URL and such.

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);
    }
}

When the user logs in, the login request is handled by 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();
    }
}

If I'm missing anything or need to explain what happens a bit more let me know.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!