Skip to main content

Check if Website is Available by PHP

When you have a website and you need to check that website is available or not to confirm it still working, the following is some function to check it is available or not:

----------Technique #1

function isDomainAvailible($domain)
{
    //check, if a valid url is provided
    if(!filter_var($domain, FILTER_VALIDATE_URL))
    {
        return false;
    }

    //initialize curl
    $curlInit = curl_init($domain);
    curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10);
    curl_setopt($curlInit,CURLOPT_HEADER,true);
    curl_setopt($curlInit,CURLOPT_NOBODY,true);
    curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);

    //get answer
    $response = curl_exec($curlInit);

    curl_close($curlInit);

    if ($response) return true;

    return false;
}


----------Technique #2

function Visit($url){
    $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";$ch=curl_init();
    curl_setopt ($ch, CURLOPT_URL,$url );
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch,CURLOPT_VERBOSE,false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch,CURLOPT_SSLVERSION,3);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
    $page=curl_exec($ch);
    //echo curl_error($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if($httpcode>=200 && $httpcode<300) return true;
    else return false;
}
if (Visit("http://www.google.com"))
    echo "Website OK"."n";
else
    echo "Website DOWN";


---------Technique #3

ini_set("default_socket_timeout","05");
set_time_limit(5);
$f=fopen("https://css-tricks.com","r");
$r=fread($f,1000);
fclose($f);
if(strlen($r)>1) {
    echo("<span class='online'>Online</span>");
}
else {
    echo("<span class='offline'>Offline</span>");
}

Comments

Popular posts from this blog

Sample VS code setting

  When you are a software developer, you need to configure your IDE working with most convinience for your working, bellow is a sample code snippet to config your IDE {   "diffEditor.ignoreTrimWhitespace" : false ,   "javascript.updateImportsOnFileMove.enabled" : "always" ,   "[typescriptreact]" : {       "editor.defaultFormatter" : "esbenp.prettier-vscode"   },   "editor.formatOnPaste" : true ,   "workbench.settings.applyToAllProfiles" : [],   "editor.tabSize" : 2 ,   "redhat.telemetry.enabled" : true ,   "editor.codeActionsOnSave" : {       },   // "editor.codeActionsOnSave": {   //   "source.fixAll": "explicit",   //   "source.fixAll.eslint": "explicit",   //   "source.organizeImports": "explicit",   //   "source.sortMembers": "explicit",   //   "javascript.showUnused": "...

Docker Compose: Node.js Express and MongoDB example

  Docker   provides lightweight containers to run services in isolation from our infrastructure so we can deliver software quickly. In this tutorial, I will show you how to dockerize Nodejs Express and MongoDB example using   Docker Compose .