dongtan6206 2011-06-23 11:33
浏览 39

PHP卷曲和setcookie问题

I have a curl script that acts as proxy between client and main server.

......

$field_array= array(
      'Accept' => 'HTTP_ACCEPT',
      'Accept-Charset' => 'HTTP_ACCEPT_CHARSET',
      'Accept-Encoding' => 'HTTP_ACCEPT_ENCODING',
      'Accept-Language' => 'HTTP_ACCEPT_LANGUAGE',
      'Connection' => 'HTTP_CONNECTION',
      'Host' => 'HTTP_HOST',
      'Referer' => 'HTTP_REFERER',
      'User-Agent' => 'HTTP_USER_AGENT'
      );

$curl_request_headers=array();

foreach ($field_array as $key => $value) {
   if(isset($_SERVER["$value"])) {
      $server_value=$_SERVER["$value"];
      $curl_request_headers[]="$key: $server_value";
   }
};
$curl_request_headers[]="Expect: ";

session_write_close();


//Open connection
$curl_handle = curl_init();
curl_setopt($curl_handle,CURLOPT_COOKIE,session_name()."=".session_id().";");
//Set the url, POST data
curl_setopt($curl_handle, CURLOPT_URL, $curl_url);
curl_setopt($curl_handle, CURLOPT_POST, !empty($user_post_data));
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $user_post_data);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($curl_handle, CURLOPT_HEADER, 1);
curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $curl_request_headers);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);

$result = curl_exec($curl_handle);
//Close connection
curl_close($curl_handle);
list($headers,$content)=explode("

",$result,2);
foreach (explode("
",$headers) as $hdr) {
   if(preg_match("/Transfer-Encoding:.*chunked/i", $hdr)) {
      // Remove chunked headers. Not properly handled by browsers
   } else {
      header($hdr);
   };
}
echo $content;

Now, on main server, I set a cookie in an script and then try to read its value in another script. I cannot read the value. So there is some problem passing the value around in curl. How to fix?

thanks

FOUND SOLUTION:

Actually, a stupid problem. I need to explicitly set cookies in CURLOPT_COOKIE. Following code now works for me:

......
$_COOKIE[session_name()]=session_id();

$cookie_string="";
foreach( $_COOKIE as $key => $value ) {
  $cookie_string .= "$key=$value;";
};

//Open connection
$curl_handle = curl_init();
curl_setopt($curl_handle,CURLOPT_COOKIE, $cookie_string);
......
  • 写回答

2条回答 默认 最新

  • dongwei8440 2012-03-08 20:51
    关注

    you have to add space after semicolon so the best way is:

    $cookie = array();
    foreach( $_COOKIE as $key => $value ) {
      $cookie[] = "{$key}={$value}";
    };
    
    $cookie = implode('; ', $cookie);
    
    $curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_COOKIE, $cookie);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答