If we make awebsite navigationwebsite, and some of the URLs included in our website are invalid. If users cannot access the website, it will give users a very bad experience!
So, how do you use php code to determine whether the url link can be accessed normally?
If you cannot access it normally, you will be given a reminder that the website has expired.
Of course, the following code cannot be guaranteed to be fully effective. For example, if you include a website that can be accessed abroad but cannot be accessed domestically, you will also be judged as "the link has expired."
function 主题ID_check_url($url){
$httpcode = 0;
$ch = curl_init();
$weburl ='';
$timeout = 1; // 设置超时的时间[单位:秒]
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch,CURLOPT_URL,$url);
curl_exec($ch);
# 获取状态码赋值
$httpcode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode == 200){
$weburl .= '<a target="_blank" rel="nofollow" href="$url">访问网站</a>';
}else{
$weburl .= '<a href="javascript:;">链接已失效</a>';
}
return $weburl;
}






add friends