使用PHP curl发送存储在$ _COOKIE全局上的Cookie [英] Sending cookies stored on $_COOKIE global using PHP curl
问题描述
我有两个网站 dev1.test.com 和 dev2.test.com 。
这些是在不同服务器上运行的两个站点。 dev1.test.com是我登录的位置,我将Cookie设置为 *。test.com 以验证用户是否已登录。
I have two site dev1.test.com and dev2.test.com.
These are two sites running on different servers. dev1.test.com is where I logged in and I have cookies set to *.test.com to validate if the user is logged.
现在在dev2.test.com上,我想通过向dev1.test.com发送PHP CURL请求来检查当前用户是否已登录。在我的curl请求中,我想将$ _COOKIE(它具有* .test.com的cookie信息)的内容包含到此curl请求中。
Now on dev2.test.com, I want to check if the current user is logged-in by sending a PHP CURL request to dev1.test.com. In my curl request, I want to include the contents of $_COOKIE (where it has the cookie information of *.test.com) to this curl request.
我应该如何处理php curl?
How should I do that in php curl?
推荐答案
由于您有一个通配符的cookie域,dev2也将有与dev1相同的cookie。所以基本上你需要说通过curl将我的cookie传递给其他服务器。
As you have a wildcard cookie domain, dev2 will also have the same cookies as dev1. So basically you need to say "pass my cookies to the other server through curl.
你想要的curl选项是CURLOPT_COOKIE,你传递一个cookie的字符串name1 = value1; name2 = value2
The curl option you want is "CURLOPT_COOKIE" and you pass in an string of the cookies "name1=value1;name2=value2"
将这些放在一起(未测试 - 当然需要将其包含在其他curl函数中)
Putting this together (untested - you need to wrap this amongst the other curl functions, of course)
$cookiesStringToPass = '';
foreach ($_COOKIE as $name=>$value) {
if ($cookiesStringToPass) {
$cookiesStringToPass .= ';';
}
$cookiesStringToPass .= $name . '=' . addslashes($value);
}
curl_setopt ($ch, CURLOPT_COOKIE, $cookiesStringToPass );
这篇关于使用PHP curl发送存储在$ _COOKIE全局上的Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!