【发布时间】:2024-11-14 14:15:02
【问题描述】:
我目前正在使用下面的方法来检查一个url是否存在
$url = 'https://www.facebook.com/a-test-example-232397848665383511';
$headers = @get_headers($url);
if(strpos($headers[0],'200')===false){
print('NOT found!');
} else {
print('found!');
}
这会打印NOT found!,即使页面在访问时清楚地解析。我打印了标题,发现这是因为它返回了302。有没有办法通过strpos 来测试所有可能解析的标头值?
标题的当前输出:
Array
(
[0] => HTTP/1.1 302 Found
[1] => Location: https://www.facebook.com/unsupportedbrowser
[2] => Vary: Accept-Encoding
[3] => Content-Type: text/html
// more array items
如果我输入一个我知道失败的网址,我会得到以下信息:
Array
(
[0] => HTTP/1.1 404 Not Found
[1] => P3P: CP="Facebook does not have a P3P policy."
[2] => Strict-Transport-Security: max-age=15552000; preload
// rest of array
简单地测试 404 是否安全?
【问题讨论】:
-
是的,您可以使用两个带有
||或条件的strpos检查来使if检查更钝。或者使用正则表达式。
标签: php http-headers