Question

I'm trying to drop invalid URLs from my flash games site. Here is my code:

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

that code return true even Content-Type are not swf .

by the way I already tried

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

but give me no result . When I tried

var_dump($headers);

return this for valid 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) "Date: Sat, 01 Feb 2014 01:36:35 GMT" [2]=> string(144) "Server: 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(20) "Accept-Ranges: bytes" [4]=> string(22) "Content-Length: 342771" [5]=> string(39) "Cache-Control: max-age=62208000, public" [6]=> string(38) "Expires: Mon, 03 Mar 2014 01:36:35 GMT" [7]=> string(17) "Connection: close" [8]=> string(43) "Content-Type: application/x-shockwave-flash" }

AND this for the Invalid 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) "Date: Sat, 01 Feb 2014 01:40:06 GMT" [2]=> string(144) "Server: 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]=> string(38) "Expires: Thu, 19 Nov 1981 08:52:00 GMT" [5]=> string(77) "Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0" [6]=> string(16) "Pragma: no-cache" [7]=> string(62) "Set-Cookie: PHPSESSID=359cf391842876b3cc79066dcc3a08f4; path=/" [8]=> string(21) "Vary: Accept-Encoding" [9]=> string(52) "Cache-Control: max-age=600, private, must-revalidate" [10]=> string(17) "Connection: close" [11]=> string(23) "Content-Type: text/html" }

So their is any easier way to get correct Content-Type of URL .

Looks like I used get_headers() in numeric only . this code from Sean Johnson works

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

La solution

According to the very first example of the get_headers documentation you need to use the second argument if you want to be able to access the header by it's key value.

Try this:

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

Autres conseils

Your code is assuming that the Content-Type header will always be the 9th header returned by the server, which is not the case.

You will need to loop through the headers and examine only the correct one (that is, the one that starts with Content-Type:).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top