Use php to develop a small module that checks whether a website is running properly & nbsp; & nbsp; my idea is to construct an http request and check its return status code. If it is 200, use php to develop a small module to check whether a website is running normally.
My idea is to construct an http request and check its return status code. If it is 200, it proves that the website can be accessed normally.
A function called curl is used to obtain the status code,
However, a problem was found. When I run only once, I can return code 200. if I continuously detect multiple websites, I only return 0. if the website fails, I can return code 404.
But the normal website still returns 0. who has used this function? can you tell me?
------ Solution --------------------
You can also use the get_headers function, as shown in the DEMO below.
Header ('content-Type: text/html; Charset = UTF-8 ');
$ Result = @ get_headers ('http: // www.qqhaowan.com ');
If ($ result ){
Print_r ($ result );
If (strpos ($ result [0], '123 ')){
Echo 'website accessible! ';
} Else {
Echo 'website inaccessible! ';
}
} Else {
Echo 'the target URL cannot be opened! ';
}
------ Solution --------------------
Reference:
My idea is to construct an http request and check its return status code. If it is 200, it proves that the website can be accessed normally.
A function called curl is used to obtain the status code,
However, a problem was found. When I run only once, I can return code 200. if I continuously detect multiple websites, I only return 0. if the website fails, I can return code 404.
But the normal website still returns 0. who has used this function? can you tell me?
Tested and can be detected cyclically.
$sites = array('http://www.baidu.com','http://www.21cn.com','http://www.sina.com.cn','http://www.csdn.net');
foreach($sites as $site){
echo $site.':'.checksite($site).'
';
}
function checksite($url){
$result = @get_headers($url);
if($result){
if(strstr($result[0], '200')!=''){
return true;
}
}
return false;
}
?>