dpt8910 2012-04-02 00:27
浏览 83
已采纳

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

I have 2 sites that are the same, but different databases and data. One site is www.somesite.com and the other is sub.somesite.com. What I am doing is if someone logs into www.somesite.com and the login details don't match www.somesite.com, but they do match sub.somesite.com, then use cURL to submit the information and log them in. I have the cURL working great:

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);

What this does is collects the post variables, submits them to the other site and adds a base url so that the paths are correct.

The problem that I am having is any other page after. So say I am on somesite.com, submit the data. Even though it is going to sub.somesite.com/welcome.php, it displays in the address bar somesite.com and the information is stored in the cookies.txt. Now when I click a link (sub.somesite.com/page2.php) on sub.somesite.com/welcome.php, it redirects to the login page because the sessions are in the cookies.txt and now aren't accessible.

How would I make the sessions continue to work on all pages?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • duai0935 2012-04-02 01:29
    关注

    It is because CURL executes it on the server not on the client (browser). Your server might have the users cookies but the user does not.

    You want to set the cookies for the browser using setcookie and forget about the Curl.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效