【问题标题】:How to grab cookies from remote URL and send them back to the browser如何从远程 URL 获取 cookie 并将它们发送回浏览器
【发布时间】:2013-12-20 08:27:50
【问题描述】:

我正在构建一个简单的 php 代理来缓存响应头和对象。

我的问题是,如果我登录到 youtube.com,然后我看不到自己,因为我已签名并且 youtube 一直说登录(未签名),但是如果我停止我的脚本并打开 youtube.com 网站,那么我会看到本人如签。我认为这是一个 cookie 问题。是吗?

我的脚本只是抓取响应头并将它们发送回浏览器。当我使用 fopen() 下载对象时,即使我通过 $_SERVER['HTTP_USER_AGENT '] 并将其附加到 $context 函数 stream_context_create() .. 仍然没有运气!.

我在响应标头中看到了这样的标头 Set-Cookie,所以我想如果我使用 header() 将它发送回浏览器,那么它就会解决.. 仍然没有运气。

这就是我抓取标题并将它们发送回浏览器的方式:

这就是我获取客户端请求的 cookie 的方式 $requested_cookie = $_COOKIE;

  $requested_cookie = $_COOKIE;
  $ua=  $_SERVER['HTTP_USER_AGENT'];
  ini_set('user_agent', $ua);
  $md5_fname = md5($fname);
  $filehead = $file_path."/HTTP_HEADER/".$md5_fname.".txt";
if (file_exists($filehead)) 
{

 $handle = fopen($filehead, "r");
 $contents = fread($handle, filesize($filehead));
 $getCachedH = unserialize($contents);
 fclose($handle);
 foreach ($getCachedH as $cHead)
 {
     $sendHead = $cHead;
     $getHead = strtoupper($cHead);
     //file_put_contents("$file_path/ghassan.txt", "\n $sendHead \n" ,FILE_APPEND);
     if ( preg_match('/CONTENT-LENGTH: (\d+)/i',$getHead,$clm) ) $content_length = $clm[1];
     if ( preg_match('/CONTENT-TYPE: ([\/\w\-\d]+)/i',$getHead,$ctm) ) $content_type = $ctm[1];
     if ( preg_match('/LOCATION: (.*)/i',$cHead,$lm) ) 
     {
         $header_location = $lm[1];
         header("Location: ".$header_location);
         exit;
     }
     if ( preg_match('/^HTTP\/1\.[01] (\d\d\d)/i', $getHead, $hcm ) ) 
     {
         $http_code = $hcm[1];

     }
     header($sendHead);
  }
 }
else
{
     $opts = array(
      'http'=>array(
       'ignore_errors'=>"true",
       'method'=>"GET",
       'header'=>"Cookie: foo=bar\r\n"
       )
      );

     $context = stream_context_create($opts);
     $urlptr = fopen($_GET['url'],'rb', false, $context);
     $headers = $http_response_header;
     $http_code = $headers[0];
     if($http_code=="200")
     {
         // We grab the Response Headers and save them 
         file_put_contents($filehead, serialize($headers));
     }

     foreach ($headers as $response_header)
     {
         $sendHead = $response_header;
         $getHead = strtoupper($response_header);
         header($sendHead);
         if ( preg_match('/CONTENT-LENGTH: (\d+)/i',$getHead,$clm) ) $content_length = $clm[1];
         if ( preg_match('/CONTENT-TYPE: ([\/\w\-\d]+)/i',$getHead,$ctm) ) $content_type = $ctm[1];
        if ( preg_match('@Set-Cookie: (([^=]+)=[^;]+)@i', $sendHead , $cookm))
         {
           $sCookies = $cookm[1];
           //file_put_contents("$file_path/cookies.txt", "\n $cookm[0], $url \n" ,FILE_APPEND);
         }
         if ( preg_match('/LOCATION: (.*)/i',$sendHead,$lm) ) 
         {
             $header_location = $lm[1];
             header("Location: ".$header_location);
             exit;
         }
         if ( preg_match('/ACCEPT-RANGES: ([\w\d\-]+)/i',$getHead,$arm) ) $accept_ranges = $arm[1];
         if ( preg_match('/^HTTP\/1\.[01] (\d\d\d)/i', $getHead, $hcm ) ) 
         {
             $http_code = $hcm[1];
         } 

      }
  }
  • 是否有一个函数可以在一次调用中获取响应头 + 对象并将它们一起存储在一个文件中?我不想在脚本顶部使用 fopen() 因为 Apache 或 Php 在读取诸如 $handler = fopen($urlptr,'r'); 之类的字符串时然后即使我没有调用字符串,它也会连接远程 URL!这增加了延迟。

  • 当我通过我的脚本从 Play 商店下载文件时,如果我已经发送 Android 设备的用户代理,是否有通过我的 php 脚本访问客户端 cookie 的解决方案以及如何解决 403 禁止消息? .

谢谢

【问题讨论】:

    标签: php android cookies


    【解决方案1】:

    我已经解决了我的问题。

    虽然我正在获取每个网站的远程标头..标头已经有 Set-Cookie 标头,所以我将它们重新发送回浏览器并从 $_COOKIE 捕获 cookie 并使用 fopen() 添加 cookie,这似乎解决了我所有的问题以上。

    谢谢

    【讨论】:

    • 注意:未定义变量:fname in /home/www/gt.php on line 6 注意:未定义变量:file_path in /home/www/gt.php on line 7 注意:未定义索引:url在第 47 行的 /home/www/gt.php 中警告:fopen():第 47 行的 /home/www/gt.php 中的文件名不能为空 注意:未定义的变量:/home/www/gt.php 中的 http_response_header第 48 行警告:在第 56 行的 /home/www/gt.php 中为 foreach() 提供的参数无效
    猜你喜欢
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多