質問
これは私のコードです:
<?php
$url = "http://www.uhasselt.be/collegeroosters/2009_2010_298_5_10.html";
$headers = get_headers($url, 1);
print_r($headers);
$contloc = $headers["Content-Location"];
echo "Content-Location: " . $contloc . "\n";
$soft404test = strpos($contloc, "http://www.uhasselt.be/404b.htm") ? true : false;
var_dump($soft404test);
?>
これはその出力です:
Array
(
[0] => HTTP/1.1 200 OK
[Content-Length] => 2030
[Content-Type] => text/html
[Content-Location] => http://www.uhasselt.be/404b.htm?404;http://www.uhasselt.be:80/collegeroosters/2009_2010_298_5_10.html
[Last-Modified] => Mon, 22 Aug 2005 07:10:22 GMT
[Accept-Ranges] => bytes
[ETag] => "88a8b68fe8a6c51:31c9e"
[Server] => Microsoft-IIS/6.0
[MicrosoftOfficeWebServer] => 5.0_Pub
[X-Powered-By] => ASP.NET
[Date] => Tue, 24 Nov 2009 08:40:25 GMT
[Connection] => close
)
Content-Location: http://www.uhasselt.be/404b.htm?404;http://www.uhasselt.be:80/collegeroosters/2009_2010_298_5_10.html
bool(false)
この動作は予想外です。私がやっていると思ったのは、HTTPヘッダーのコンテンツロケーション属性を見ることでソフト404を検出することです。 STRPOS関数は、私が得られない決定を下します。どこで私は間違えましたか? (ちなみに、他のサイトで動作するためにこれは必要ありません。)
解決
strpos()
文字列が見つからない場合はfalseを返すことができます。ただし、0はブールチェックでfalseと評価するため、タイプを明示的にチェックする必要があります。
$soft404test = strpos($contloc, "http://www.uhasselt.be/404b.htm") !== false;
所属していません StackOverflow