【发布时间】: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 禁止消息? .
谢谢
【问题讨论】: