【问题标题】:Check if swf url avaliable or not using php get_headers Content-Type使用 php get_headers Content-Type 检查 swf url 是否可用
【发布时间】:2014-02-24 22:06:00
【问题描述】:

我正在尝试从我的 Flash 游戏网站中删除无效的 URL。 这是我的代码:

function valid($URL1) {
$headers = get_headers($URL1);
$headers = substr($headers[8],38,5);//leaves only flash word 
if ($headers=='flash')
return true; else   return false;
}
$URL1='http://www.ht83.com/medias/media-16/ht83com-cde-house-decoration.swf';
if(valid($URL1))
echo 'SWF are word>' ;

即使 Content-Type 不是 swf,代码也会返回 true。

我已经试过了

$headers=$headers['Content-Type'];

但不给我任何结果。 当我尝试

var_dump($headers);

返回这个以获得有效的 SWF URL http://www.ht83.com/medias/media-16/ht83com-spongebob-squarepants-gone-fishing.swf

array(9) { [0]=> string(15) "HTTP/1.1 200 OK" [1]=> string(35) "日期: 2014 年 2 月 1 日星期六 01:36:35 GMT" [2]=> string(144) "服务器: Apache/2.2.17 (Unix) mod_ssl/2.2.17 OpenSSL/0.9.8m DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 mod_fcgid/2.3.5" [3]=> 字符串(20) "接受范围:字节" [4]=> 字符串(22)“内容长度:342771”[5]=>字符串(39)“缓存控制: max-age=62208000, public" [6]=> string(38) "过期时间:星期一,2014 年 3 月 3 日 格林威治标准时间 01:36:35”[7]=> 字符串(17)“连接:关闭”[8]=> 字符串(43) “内容类型:应用程序/x-shockwave-flash”}

此为无效的 SWF URL http://www.ht83.com/medias/media-16/ht83com-cde-house-decoration.swf

array(12) { [0]=> string(15) "HTTP/1.1 200 OK" [1]=> string(35) "日期: 2014 年 2 月 1 日星期六 01:40:06 GMT" [2]=> string(144) "服务器: Apache/2.2.17 (Unix) mod_ssl/2.2.17 OpenSSL/0.9.8m DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 mod_fcgid/2.3.5" [3]=> string(24) "X-Powered-By: PHP/5.2.16" [4]=> 字符串(38)“到期:星期四,1981 年 11 月 19 日 08:52:00 GMT”[5]=> 字符串(77) “缓存控制:无存储,无缓存,必须重新验证,检查后 = 0, 预检查=0" [6]=> 字符串(16) "Pragma: no-cache" [7]=> 字符串(62) “设置 Cookie:PHPSESSID=359cf391842876b3cc79066dcc3a08f4;路径=/”[8]=> 字符串(21)“变化:接受编码”[9]=>字符串(52)“缓存控制: max-age=600, private, must-revalidate" [10]=> string(17) "连接: 关闭" [11]=> 字符串(23) "内容类型:文本/html" }

因此,他们是获得正确 URL 内容类型的任何更简单的方法。

看起来我只在数字中使用了 get_headers() 。来自 Sean Johnson 的这段代码有效

function valid($URL) {
$headers = get_headers($URL, 1);//
return stripos($headers['Content-Type'],"application/x-shockwave-flash")!==false;
}

【问题讨论】:

  • this 这个正确的函数 function valid($URL) { $headers = get_headers($URL,1); return stripos("flash",$headers['Content-Type'])!==false; }

标签: php string content-type get-headers


【解决方案1】:

根据get_headers documentation 的第一个示例,如果您希望能够通过其键值访问标头,则需要使用第二个参数。

试试这个:

function valid($URL) {
    $headers = get_headers($URL,1);
    return stripos($headers['Content-Type'],"flash")!==false;
}

【讨论】:

  • 感谢您的代码完美运行,除了使用“flash”进行的小修复,它应该看起来像。 "application/x-shockwave-flash" 现在该功能正常工作了:)
  • 我已经编辑了代码,以便它可以在标题中的任何位置找到“flash”。 :)
【解决方案2】:

您的代码假设 Content-Type 标头始终是服务器返回的第 9 个标头,但事实并非如此。

您需要遍历标头并仅检查正确的标头(即以Content-Type: 开头的标头)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    相关资源
    最近更新 更多