I'm working in a PHP project that uses subdomains, sessions and Ajax. But unfortunately I can't make it work! I'll try explain:
我正在使用子域,会话和Ajax的PHP项目。但不幸的是我无法让它发挥作用!我会试着解释一下:
Let's assume that I'm at this domain: app.mysite.com/index.php
我们假设我在这个域:app.mysite.com/index.php
At this domain, I have a form that performs an Ajax request to mysite.com/functions/execute.php (without any subdomain)
在这个域中,我有一个表单,它对mysite.com/functions/execute.php执行Ajax请求(没有任何子域)
In the first line of execute.php, I have a require_once that include a helper.php file. In this file I have put:
在execute.php的第一行,我有一个包含helper.php文件的require_once。在这个文件中我放了:
ini_set('session.cookie_domain', '.mysite.com');
session_set_cookie_params(0, '/', '.mysite.com');
session_start();
All PHP files listed also include the helper.php.
列出的所有PHP文件还包括helper.php。
If I for example run:
如果我举例如:
echo $_SESSION["myValue"];
At app.mysite.com/index.php or any other subdomain, like auth.mysite.com, I'll get the value: "test". But if I run the same code at execute.php, and return the value through Ajax I'll get undefined index!
在app.mysite.com/index.php或任何其他子域,如auth.mysite.com,我将获得值:“test”。但是如果我在execute.php上运行相同的代码,并通过Ajax返回值,我将得到未定义的索引!
What am I doing wrong?
我究竟做错了什么?
3 个解决方案
#1
1
Please refer this link. I hope it's will help you.
请参阅此链接。我希望它会对你有所帮助。
Setting a cookie on a subdomain from an ajax request
从ajax请求在子域上设置cookie
I do not see SESSION vars when calling subdomain script with Jquery (ajax)
用Jquery(ajax)调用子域脚本时,我没有看到SESSION变量
Thank You!
谢谢!
#2
1
If your project is web based application you can easily set cookie/session in all domain with a simple trick. Am sure this works for cookies but never tried with sessions. Lets do what Google is doing. Create a PHP file that sets the cookie on all 3 domains. Then on the domain where the theme is going to set, create a HTML file that would load the PHP file that sets cookie on the other 2 domains. Example:
如果您的项目是基于Web的应用程序,您可以通过简单的技巧轻松地在所有域中设置cookie /会话。我确定这适用于cookie但从未尝试过会话。让我们做谷歌正在做的事情。创建一个PHP文件,在所有3个域上设置cookie。然后在主题要设置的域上,创建一个HTML文件,该文件将加载在其他2个域上设置cookie的PHP文件。例:
<html>
<head></head>
<body>
<p>Please wait.....</p>
<img src="http://domain2.com/setcookie.php?theme=whateveryourthemehere" />
<img src="http://domain3.com/setcookie.php?theme=whateveryourthemehere" />
</body>
</html>
Keep those img
elements hidden so that it will not show any broken image in the front end if page is visible to user.Then add an onload callback on body tag. The document will only load when the images completely load that is when cookies are set on the other 2 domains. Onload Callback :
保持这些img元素隐藏,以便在用户可以看到页面时,它不会在前端显示任何损坏的图像。然后在body标签上添加onload回调。只有当图像完全加载时才会加载文档,即在其他2个域上设置cookie时。 Onload回调:
<head>
<script>
function loadComplete(){
window.location="http://domain1.com";//URL of domain1
}
</script>
</head>
<body onload="loadComplete()">
We set the cookies on the other domains using a PHP file like this(setcookie.php ) :
我们使用像这样的PHP文件(setcookie.php)在其他域上设置cookie:
<?php
if(isset($_GET['theme'])){
setcookie("theme", $_GET['theme'], time()+3600);
}
?>
Now cookies are set on the three domains:) and with web application you know how retrieve cookie:)
现在cookie设置在三个域:)和Web应用程序,你知道如何检索cookie :)
Ofcourse you may need to tweek in this code as per your requirements. But this this will definitely give you an idea to proceed
当然,您可能需要根据您的要求调整此代码。但这肯定会让你有机会继续前进
Hope this helps
希望这可以帮助
#3
1
I already figure out how to make this work. Ajax Post method do not send credentials header by default, so we need to enable manually:
我已经弄清楚如何使这项工作。 Ajax Post方法默认不发送凭证头,因此我们需要手动启用:
$.ajax({
method : "POST",
url : "https://example.com/functions/execute.php",
data : myData,
xhrFields: {
withCredentials: true
}
}).done(function(result) {
alert("success"));
});
And in execute.php you need to put:
在execute.php中你需要放:
ini_set('session.cookie_domain', '.example.com');
session_set_cookie_params(0, '/', '.example.com');
session_start();
header('Access-Control-Allow-Credentials: true');
And if you request this from a subdomain, also need to put at example.php:
如果你从子域请求这个,也需要放在example.php:
header('Access-Control-Allow-Origin: http://app.example.com');
#1
1
Please refer this link. I hope it's will help you.
请参阅此链接。我希望它会对你有所帮助。
Setting a cookie on a subdomain from an ajax request
从ajax请求在子域上设置cookie
I do not see SESSION vars when calling subdomain script with Jquery (ajax)
用Jquery(ajax)调用子域脚本时,我没有看到SESSION变量
Thank You!
谢谢!
#2
1
If your project is web based application you can easily set cookie/session in all domain with a simple trick. Am sure this works for cookies but never tried with sessions. Lets do what Google is doing. Create a PHP file that sets the cookie on all 3 domains. Then on the domain where the theme is going to set, create a HTML file that would load the PHP file that sets cookie on the other 2 domains. Example:
如果您的项目是基于Web的应用程序,您可以通过简单的技巧轻松地在所有域中设置cookie /会话。我确定这适用于cookie但从未尝试过会话。让我们做谷歌正在做的事情。创建一个PHP文件,在所有3个域上设置cookie。然后在主题要设置的域上,创建一个HTML文件,该文件将加载在其他2个域上设置cookie的PHP文件。例:
<html>
<head></head>
<body>
<p>Please wait.....</p>
<img src="http://domain2.com/setcookie.php?theme=whateveryourthemehere" />
<img src="http://domain3.com/setcookie.php?theme=whateveryourthemehere" />
</body>
</html>
Keep those img
elements hidden so that it will not show any broken image in the front end if page is visible to user.Then add an onload callback on body tag. The document will only load when the images completely load that is when cookies are set on the other 2 domains. Onload Callback :
保持这些img元素隐藏,以便在用户可以看到页面时,它不会在前端显示任何损坏的图像。然后在body标签上添加onload回调。只有当图像完全加载时才会加载文档,即在其他2个域上设置cookie时。 Onload回调:
<head>
<script>
function loadComplete(){
window.location="http://domain1.com";//URL of domain1
}
</script>
</head>
<body onload="loadComplete()">
We set the cookies on the other domains using a PHP file like this(setcookie.php ) :
我们使用像这样的PHP文件(setcookie.php)在其他域上设置cookie:
<?php
if(isset($_GET['theme'])){
setcookie("theme", $_GET['theme'], time()+3600);
}
?>
Now cookies are set on the three domains:) and with web application you know how retrieve cookie:)
现在cookie设置在三个域:)和Web应用程序,你知道如何检索cookie :)
Ofcourse you may need to tweek in this code as per your requirements. But this this will definitely give you an idea to proceed
当然,您可能需要根据您的要求调整此代码。但这肯定会让你有机会继续前进
Hope this helps
希望这可以帮助
#3
1
I already figure out how to make this work. Ajax Post method do not send credentials header by default, so we need to enable manually:
我已经弄清楚如何使这项工作。 Ajax Post方法默认不发送凭证头,因此我们需要手动启用:
$.ajax({
method : "POST",
url : "https://example.com/functions/execute.php",
data : myData,
xhrFields: {
withCredentials: true
}
}).done(function(result) {
alert("success"));
});
And in execute.php you need to put:
在execute.php中你需要放:
ini_set('session.cookie_domain', '.example.com');
session_set_cookie_params(0, '/', '.example.com');
session_start();
header('Access-Control-Allow-Credentials: true');
And if you request this from a subdomain, also need to put at example.php:
如果你从子域请求这个,也需要放在example.php:
header('Access-Control-Allow-Origin: http://app.example.com');