2009-11-24 81 views
1

這是我的代碼:奇strpos行爲

<?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頭中的Content-Location屬性來檢測軟404。 strpos函數做出我沒有得到的決定。我哪裏做錯了? (順便說一下,我不需要在其他站點上工作)。

回答

6

strpos()如果找不到字符串,則返回false;如果在最開始時找到字符串,則返回0。然而,0在布爾檢查中評估爲false,因此您需要明確檢查類型:

$soft404test = strpos($contloc, "http://www.uhasselt.be/404b.htm") !== false;