本文介绍了PHP多域会话ini_set不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图设置它,所以如果你登录到我的网站会话转移到我的网站的所有子域。例如,如果您访问domain.com并登录,请访问sub.domain.com,您将已经在sub.domain.com上登录。



根据我的理解,你需要使用ini_set('session.cookie_domain','。domain.com')和session_start(),然后设置你的会话变量,但这是不行的。



我在做什么的例子:



domain.com的代码:

 <?php 
ini_set('session.cookie_domain','。domain.com');
session_start();
$ _SESSION ['variable'] = 1;
?>

sub.domain.com的代码:

 <?php 
session_start();
echo $ _SESSION ['variable'];
?>

但$ _SESSION ['variable']未设置。



我也尝试在sub.domain.com代码中使用ini_set(),但没有任何区别。我已经通过使用ini_get()来验证设置session.cookie_domain是否正常工作。



我做错了什么?谢谢!

解决方案

首先验证ini_set

 code><?php 
ini_set('session.cookie_domain','。domain.com');

echo ini_get('session.cookie_domain');

session_start();
$ _SESSION ['variable'] = 1;

?>






更新:



只是想一想..你还试过:

   
session_set_cookie_params(0,/,.domain.com,false,false);
session_start();
$ _SESSION ['variable'] = 1;

?>






更新2:ALternate处理(手动cookie处理)

 <?php 

session_start();
session_regenerate_id();
$ _SESSION ['variable'] =String Test;

setcookie('PHPSESSID',session_id(),time()+ 86400,'/','。domain.com');
echo session_id();
?>

并在子域文件中



<$ p $ ($ _ COOKIE ['PHPSESSID'])&空($ _COOKIE ['PHPSESSID']))session_id($ _ COOKIE [' PHPSESSID']);

session_start();
echo $ _SESSION ['variable']。 < br />;
echo $ _COOKIE ['PHPSESSID']。 < br />;
echo session_id();
?>






可以添加到每个文件中的三行关闭/处理会话信息

  if(isset($ _ COOKIE ['PHPSESSID'])&&空($ _ COOKIE ['PHPSESSID']))session_id($ _ COOKIE ['PHPSESSID']); 
session_start();
if(!isset($ _ COOKIE ['PHPSESSID']))setcookie('PHPSESSID',session_id(),time()+ 86400,'/','。domain.com');

你通过会话有什么信息?还是使用它来处理登录等?


I'm trying to set it up so if you log in to my website the session carries over to all sub-domains of my website. For example, if you go to domain.com and log in, then go to sub.domain.com, you'll already be logged in at sub.domain.com.

To my understanding, you would want to use ini_set('session.cookie_domain','.domain.com') and then session_start(), then set your session variables, but this isn't working.

Example of what I'm doing:

Code for domain.com:

<?php
 ini_set('session.cookie_domain','.domain.com');
 session_start();
 $_SESSION['variable'] = 1;
?>

Code for sub.domain.com:

<?php
 session_start();
 echo $_SESSION['variable'];
?>

But $_SESSION['variable'] isn't set.

I've also tried using ini_set() in the sub.domain.com code, but it made no difference. I've verified that setting session.cookie_domain is working by using ini_get().

What am I doing wrong? Thanks!

解决方案

First verify the ini_set

<?php
ini_set('session.cookie_domain','.domain.com');

echo ini_get('session.cookie_domain');

session_start();
$_SESSION['variable'] = 1;

?>


Update:

Just thought about it.. Did you also try:

<?php

session_set_cookie_params( 0, "/", ".domain.com", false, false);
session_start();
$_SESSION['variable'] = 1;

?>


Update 2: ALternate handling (manual cookie handling)

<?php

session_start();
session_regenerate_id();
$_SESSION['variable'] = "String Test";

setcookie('PHPSESSID',session_id(),time()+86400,'/','.domain.com');
echo session_id();
?>

and in the subdomain file

<?php
if (isset($_COOKIE['PHPSESSID']) && !empty($_COOKIE['PHPSESSID'])) session_id($_COOKIE['PHPSESSID']);

session_start();
echo $_SESSION['variable'] . "<br />";
echo $_COOKIE['PHPSESSID'] . "<br />";
echo session_id();
?>


Three lines you could add to every file to hand off / handle session info

if (isset($_COOKIE['PHPSESSID']) && !empty($_COOKIE['PHPSESSID'])) session_id($_COOKIE['PHPSESSID']);
session_start();
if (!isset($_COOKIE['PHPSESSID'])) setcookie('PHPSESSID',session_id(),time()+86400,'/','.domain.com');

What info are you passing through the session? Or are you using it to handle logins, etc?

这篇关于PHP多域会话ini_set不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 18:48