Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php cURL登录在重定向时丢失会话变量_Php_Session_Redirect_Curl_Session Cookies - Fatal编程技术网

Php cURL登录在重定向时丢失会话变量

Php cURL登录在重定向时丢失会话变量,php,session,redirect,curl,session-cookies,Php,Session,Redirect,Curl,Session Cookies,我有两个网站是相同的,但不同的数据库和数据。一个网站是www.somesite.com,另一个是sub.somesite.com。我要做的是,如果有人登录www.somesite.com,而登录详细信息与www.somesite.com不匹配,但与sub.somesite.com匹配,那么使用cURL提交信息并登录。我的卷发效果很好: foreach($_POST as $key=>$p) { if ($post != "") { $post .= "&"; }

我有两个网站是相同的,但不同的数据库和数据。一个网站是www.somesite.com,另一个是sub.somesite.com。我要做的是,如果有人登录www.somesite.com,而登录详细信息与www.somesite.com不匹配,但与sub.somesite.com匹配,那么使用cURL提交信息并登录。我的卷发效果很好:

foreach($_POST as $key=>$p) {
  if ($post != "") {
    $post .= "&";
  }
  $post .= $key."=".$p;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://sub.somesite.com"); 
curl_setopt($ch, CURLOPT_POST, count($_POST));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt"); 
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$cArray = curl_getinfo($ch);
$newURL = $cArray[url];
echo str_replace('</head>','<base href="'.$newURL.'" /></head>',$result);
curl_close($ch);
foreach($\u发布为$key=>$p){
如果($post!=“”){
$post.=“&”;
}
$post.=$key.“=”$p;
}
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,”http://sub.somesite.com"); 
curl_setopt($ch,CURLOPT_POST,count($_POST));
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
curl_setopt($ch,CURLOPT_COOKIEJAR,“cookies.txt”);
curl_setopt($ch,CURLOPT_COOKIEFILE,“cookies.txt”);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result=curl\u exec($ch);
$cArray=curl\u getinfo($ch);
$newURL=$cArray[url];
echo str_替换(“”,,$result);
卷曲关闭($ch);
这样做的目的是收集post变量,将它们提交到另一个站点,并添加一个基本url,以便路径正确

我遇到的问题是后面的任何其他页面。假设我在somesite.com上,提交数据。即使它将转到sub.somesite.com/welcome.php,它也会显示在地址栏somesite.com中,信息存储在cookies.txt中。现在,当我单击sub.somesite.com/welcome.php上的链接(sub.somesite.com/page2.php)时,它会重定向到登录页面,因为会话位于cookies.txt中,现在无法访问

我如何使会话在所有页面上继续工作


提前感谢。

这是因为CURL在服务器上而不是在客户端(浏览器)上执行它。您的服务器可能有用户cookie,但用户没有


您希望使用设置浏览器的COOKIE,而忽略Curl。

是有意义的,但它们使用$\u会话而不是$\u COOKIE。我想我将不得不改变这两个网址呢?第一个设置cookie,第二个查找cookie并转换为会话?好吧,也许将会话cookie(通常称为PHPSESSID)传递给客户端,然后重定向,会使其工作。但是忘记curl。您可以使用session\u set\u cookie\u params设置该域的会话cookie,因此我会将其放在两者的session\u start()之前,并确保它是.somesite.com作为域?在每个session\u start()之前使用session\u set\u cookie\u params并在第一个域上设置会话有效。这不完全是我想要的,但我能够用它来获得想要的结果。谢谢