Вопрос по php, session – PHP-сессии в поддоменах

87

Я пытаюсь настроить следующее:

auth.domain.com
sub1.domain.com
sub2.domain.com

где, если пользователь посещает sub1.domain.com или sub2.domain.com, и они не вошли в систему, они перенаправляются на auth.domain.com и могут войти в систему. sub1.domain.com и sub2.domain.com два отдельных приложения, но используют одинаковые учетные данные.

Я попытался установить следующее в моем php.ini:

session.cookie_domain = ".domain.com"

но, похоже, он не передает информацию из одного домена в другой.

[Edit]

Я попробовал следующее:

sub1.domain.com/test.php

session_set_cookie_params(0, '/', '.domain.com');
session_start();
print session_id() . "<br>";
$_SESSION['Regsitered'] = 1;
echo '<a href="http://auth.domain.com/test.php">Change Sites</a>'

auth.domain.com/test.php

session_set_cookie_params(0, '/', '.domain.com');
session_start();
print session_id() . "<br>";
$_SESSION['Checked'] = 1;
print_r($_SESSION);

Идентификаторы сеанса в точности совпадают, но когда я выкидываю переменную $ _SESSION, она не показывает оба ключа, а только тот ключ, который я установил в каждом домене.

[Edit 2]

Я обновил [Изменить]

Error: User Rate Limit Exceededstackoverflow.com/questions/2835486/… boksiora
Error: User Rate Limit Exceeded Milen A. Radev
Error: User Rate Limit Exceededhttp://us2.php.net/manual/en/function.session-set-cookie-params.php Residuum

Ваш Ответ

16   ответов
0

*

*

2

joreon

define('ROOT_DOMAIN',   'mysite.com');
define('PHP_SESSION_NAME', 'MYSITE'); 

session_name(PHP_SESSION_NAME);
session_set_cookie_params(0, '/', '.' . ROOT_DOMAIN);
session_start();

function load_session() {
    if (session_status() == PHP_SESSION_NONE) {
        session_name(PHP_SESSION_NAME);
        session_set_cookie_params(0, '/', '.' . ROOT_DOMAIN);
        session_start();
    }
    else {
        if (session_name() != PHP_SESSION_NAME) {
            session_destroy();
            session_name(PHP_SESSION_NAME);
            session_set_cookie_params(0, '/', '.' . ROOT_DOMAIN);
            session_start();
        }
    }
}
load_session(); // put it in anywhere you want to use session
0

session_start()

$sess_life_time = 21600; //in seconds
$sess_path = "/";
$sess_domain = ".you-site-name.com";
$sess_secure = true; // if you have secured session
$sess_httponly = true; // httponly flag

session_set_cookie_params($sess_life_time, $sess_path, $sess_domain, $sess_secure, $sess_httponly);
1

ini_set('session.cookie_domain', 
    substr($_SERVER['SERVER_NAME'],strpos($_SERVER['SERVER_NAME'],"."),100));
Error: User Rate Limit Exceeded
-2

header( $url.'?'.session_name().'='.session_id() );

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
0

session.cookie_domain = ".example.com"

suhosin.session.cryptdocroot = Off

suhosin.cookie.cryptdocroot = Off

1

sub.exampledomain.com/sessionadd.php?id=123

exampledomain.com/sessionview.php // 123

<?php 
$currentCookieParams = session_get_cookie_params(); 

$rootDomain = '.example.com'; 

session_set_cookie_params( 
    $currentCookieParams["lifetime"], 
    $currentCookieParams["path"], 
    $rootDomain, 
    $currentCookieParams["secure"], 
    $currentCookieParams["httponly"] 
); 

session_name('mysessionname'); 
session_start(); 

setcookie($cookieName, $cookieValue, time() + 3600, '/', $rootDomain); 
?>
5

session.cookie_domain = "domain.com"

session.cookie_domain = ".domain.com"

какая поддержка браузера должна здесь? это действие на стороне сервера.
Error: User Rate Limit Exceeded
2

session_name('name');
ini_set('session.cookie_domain', '.example.com');
ini_set('session.save_path', '/var/lib/php/session');
session_start();

session.save_pathsame

Error: User Rate Limit Exceeded
127

$some_name = session_name("some_name");
session_set_cookie_params(0, '/', '.some_domain.com');
session_start();

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededsession_name("domain");Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded$some_name = session_name("some_name");Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededstackoverflow.com/questions/4948340/…Error: User Rate Limit Exceeded
0

session.cookie_domainphp.ini

session.cookie_domain = '.yourdomainname.org'

session.auto_start = 1
3

ini_set('session.cookie_domain', '.testdomain.dev');
session_start();

ini_set('session.cookie_domain', '.localhost');

wasn't working

Error: User Rate Limit Exceeded
0

<?php
define('site_domain','domain.com');
session_set_save_handler('_open',
                         '_close',
                         '_read',
                         '_write',
                         '_destroy',
                         '_clean');

function _open(){

    global $_sess_db;

$db_user = 'user';
$db_pass = 'pass';
$db_host = 'localhost';

if ($_sess_db = mysql_connect($db_host, $db_user, $db_pass)){

    return mysql_select_db('database', $_sess_db);

}

return false;

}

function _close(){

    global $_sess_db;
    return mysql_close($_sess_db);

}

function _read($id){

    global $_sess_db;
    $id = mysql_real_escape_string($id);
    $domain = mysql_real_escape_string(site_domain);
    $agent = mysql_real_escape_string(isset($_SERVER['HTTP_USER_AGENT']));

    $sql = "SELECT data
    FROM sessions
    WHERE id = '$id' AND domain = '$domain' AND agent = '$agent'";

     if ($result = mysql_query($sql, $_sess_db)){

         if (mysql_num_rows($result)){
             $record = mysql_fetch_assoc($result);
             return $record['data'];
        }

    }

    return '';

}

function _write($id, $data){

    global $_sess_db;
    $access = time();

    $id = mysql_real_escape_string($id);
    $access = mysql_real_escape_string($access);
    $data = mysql_real_escape_string($data);
    $domain = mysql_real_escape_string(site_domain);
    $agent = mysql_real_escape_string(isset($_SERVER['HTTP_USER_AGENT']));

    $sql = "REPLACE INTO sessions
    VALUES ('$id', '$access', '$data', '$domain', '$agent')";

    return mysql_query($sql, $_sess_db);

}

function _destroy($id){

    global $_sess_db;
    $id = mysql_real_escape_string($id);
    $domain = mysql_real_escape_string(site_domain);
    $agent = mysql_real_escape_string(isset($_SERVER['HTTP_USER_AGENT']));

    $sql = "DELETE FROM sessions
    WHERE id = '$id' AND domain = '$domain' AND agent = '$agent'";

    return mysql_query($sql, $_sess_db);

}

function _clean($max){

    global $_sess_db;
    $old = time() - $max;
    $old = mysql_real_escape_string($old);
    $domain = mysql_real_escape_string(site_domain);
    $agent = mysql_real_escape_string(isset($_SERVER['HTTP_USER_AGENT']));

    $sql = "DELETE FROM sessions
    WHERE  access < '$old' AND domain = '$domain' AND agent = '$agent'";

    return mysql_query($sql, $_sess_db);

}

Error: User Rate Limit Exceeded
24

.domain.com

suhosin.session.cryptua = Off 
suhosin.session.cryptdocroot = Off
4

php_value session.cookie_domain .example.local

session.cookie_domainsession.cookie_domain

set the session.cookie_domain for ALL SUBDOMAINS. don't set it for the main DOMAIN

EDIT

0

Error: User Rate Limit Exceeded dragonmantank

Похожие вопросы