PHP

PHP:用 cURL 取得網站狀態碼

前言

之前網站剛架設的時候裝了一些常見外掛,像是Jetpack、WordFence、Yoast之類的,可能是我的虛擬主機太廢了,這些外掛對伺服器loading實在太重了,幾乎每隔半天就斷線,最後刪了這些外掛就恢復正常了= ="

那時候為了要偵測網站是否掛掉,就去研究可以判斷網站是否可用的PHP程式,目前用起來最容易的方式應該是cURL。

不囉嗦,直接上程式碼。

功能實現

裡面都有註解,就不解說了XD

<?php
    $host = "your url"; 
    $find = "200";  # 正常網站狀態碼是200

    function check($host, $find) {  

        // 建一個cURL
        $ch = curl_init();

        // 設置cURL參數
        curl_setopt($ch, CURLOPT_URL, $host);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // 執行cURL
        curl_exec($ch);

        // $output = curl_exec($ch);
        // if($output === FALSE ){
            // echo "CURL Error:".curl_error($ch); 
        // }

        // 取得資訊
        $info = curl_getinfo($ch);

        // 取得狀態碼
        $http_code = $info["http_code"];

        // 關閉cURL
        curl_close($ch);

        // 判斷網站是否正常
        // 正常回傳true,錯誤回傳false
        if ($http_code == $find){
            return true;
        } else {
            return false;
        }
    }

    // 顯示離線或在線
    if (!check($host, $find)){
        echo "offline";
    } else {
        echo "online";
    }

1,069 則留言

  • rzelxhrud

    JUGAR CON RESPONSABILIDAD: luckyjet-games es un sitio web independiente sin vínculos con los sitios web que promocionamos. Antes de participar en cualquier tipo de juego, asegúrate de que cumples todos los requisitos legales y los criterios de edad de tu jurisdicción. Nuestra misión aquí en luckyjetgames es proporcionar contenido informativo y de entretenimiento con fines puramente educativos – si hace clic en estos enlaces externos, saldrá de este sitio por completo. Lucky Jet es un popular juego de choque similar a Aviator. Los jugadores en línea pueden jugarlo en casinos en línea populares y miles de otros juegos de casino después de registrarse. Algunas personas hablan de una herramienta de predicción como una manera de engañar a Lucky Jet juego para maximizar sus ganancias cada vez, y más abajo se puede aprender todo sobre él.
    https://www.cnc-dijelovi.com/2025/06/03/la-app-tower-rush-entretenimiento-con-ganancias-reales/
    More information about our Cookie Policy Escucha a La Ultima Copa en español en Spotify o Apple Podcasts. England’s run to the final of a men’s international tournament outside England – the first time this has happened since the team began entering such competitions in 1950 (long after the World Cup began in 1930) – immediately grabs the attention. Finals are undoubtedly select events – and one only comes along every other year. Track integrating block (CHALLENGE) “Boasting nerves of steel and high levels of concentration, Costa is never one to back away from a challenge and does a magnificent job of organizing his backline, coming off his line to claim crosses, and getting a strong punch to clear the danger,” Lowy wrote. Full information about the upcoming eFootball.Pro Cup and the final standings of the eFootball.Pro League can be found on the official website – efootballpro.konami.net

留下一個回覆

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *