Z-blogPHP detects whether URL navigation links are normally accessible

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;
}


image.png

t like2

Link to this article:https://en.finchui.com/zblog-course/14.html

netizens commented

guess you like

hot commodity
popular articles
hot Tags
related tags
FinchUI Store Edition Hong Kong Special Area

share

copy Link

Xinglan Studioadd friends

Working hours: 9:00-22:00
Saturday and Sunday: 14:00-22:00
wechat
Scan the code to add customer service WeChat