当在大海捞针的开头找到针时,松散地检查 strpos() 的真实响应会给出错误的结果

发布于 2024-08-12 15:36:54 字数 1239 浏览 5 评论 0原文

这是我的代码:

<?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() 函数做出了我无法理解的决定。我哪里做错了? (顺便说一句,我不需要这个在其他网站上工作。)

This is my code:

<?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);
?>

This is its output:

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)

This behavior is unexpected. What I thought I was doing is detecting soft 404's by looking at the Content-Location attribute in my HTTP headers. The strpos() function makes decisions I don't get. Where did I go wrong? (I don't need this to work on other sites, by the way.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

メ斷腸人バ 2024-08-19 15:36:54

strpos() 如果找不到字符串,则返回 false;如果找不到字符串,则返回 0是在一开始就发现的。然而 0 在布尔检查中计算结果为 false,因此您需要显式检查类型:

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

strpos() can return false if the string isn't found or 0 if the string is found at the very beginning. However 0 evaluates to false in a boolean check so you need to explicitly check the type:

$soft404test = strpos($contloc, "http://www.uhasselt.be/404b.htm") !== false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文