Hi guys, I promise I already searched around a lot but I didn't find anything so I'm here to ask if someone of you knows which is the best way to check automatically if a website is working.
I was thinking about a crontab that calls a php page where with a php script (that obviously I don't know which one at the moment) it checks if a file in the website, obviously in a different server and without any support to php, exists.

Thanks a lot for any help, chr

    As you can see I'm not so keen on serverside. Do you think is easier to create a kind of application that automatically ping and in the case sends an email to notice the negative availability of the site?

    Isn't the ping just versus an ip address? We had very weird behaviors from a windows hosting and I suppose that the websites could be not available also if the ping returns a true.

    what do you think? the issues has born jsut for the bad experience we had with that hosting.

    thx, chr

      Hi Gabon

      Some - many - firewalls don't allow pinging. I just pinged PHP Builder and got a total failure. This could have a lot to do with our firewall at work. The result means nothing.

      You could write a short PHP script which scrapes the first 4KB of any webpage you're interested in into a flat file. You can then ask the script to build a list of cases in which it didn't work. Only problem is - you'd have to hard code the URLs if you're running it as a crontab. If you are checking different URLs every day, this could become a pain.

      Not sure what to suggest.

      Norm

        thx norman, which kind of script are talking about? I tried with file_exist but i think it works just inside the server... thx, chr

          Hi Gabon

          I just mean using fopen, fread etc. Nothing fancy. You specify the remote file (http://www.example.com) and read in the first few KBs of the file into a txt file. If you get an empty string, you can tell your script to list the offending URL. It's about as primitive as it gets, but it might at least eliminate functioning URLs from your enquiries. You can then attend to the ones which, at the time you ran your script, didn't work.

          Norm

            I used a kind of code I made for a counter, so:

            <?php
            $fp = fopen("http://www.mywebsite.com/file.txt","r");
            $count = fgets($fp,1024);
            fclose($fp);
            
            if($count>0){
            	echo "File Exists";
            }else{
            	echo "File NOT Exists";
            }
            ?>

            and of course in the file i put "1" but I imagine it is not the cleanest way.
            So since the reference says that the function in the case there is an error returns FALSE, I trade to print the result of a missing file:

            echo @fopen("http://www.mywebsite.com/file_that_doesnt_exist.txt","r");

            but it doesn't print anything (I was aspected a FALSE)... how can I check if the file is available or not?

            thx, chr

              Hi Gabon

              You've got the right idea. Try this:

              <?php
              $failfile = "dud_urls.txt";
              $website = "http://www.mywebsite.com/index.html"; 
              $fp = fopen($website,"r"); 
              $count = fgets($fp,1024); 
              fclose($fp); 
              
              if($count>0){ 
                  echo "File Exists"; 
              }else{ 
                  $handle = fopen($failfile, "a");    
              fwrite($handle, $website); fclose($handle); } ?>

              Of course, what you really want to do is create an array of URLs and get your code to iterate through the array, doing a check on each item. In fact, what you really, really want to do is have all the URLs in a DB table, extract the URLs as an array and then get the code to run through it. You then want to re-insert the dud URLs into another table in the DB and decide what you're going to do with them. Either that or write them to an XML file which you parse as HTML and then click on by hand. The possibilities are endless. If you do this stuff, you're well on your way to having created a PHP link checker which you can submit to Hotscripts or something.

              HTH

              Norm

                well norman... actually my idea could be to create an array of site (files) to check.
                and the in the case someone of them doesn't work send an email to the administrator listing whichones are not working...

                one question about ur script, u open an html site and then use the same code I used to read a value inside a txt:

                fgets($fp, 10240)

                does it return a number also if you load an unknown formatted html file?

                about the script on hotscript I'm happy to leave to you the honor when it will be finished 🙂 I'm keen on other kind of programming...

                thx, chr

                  Write a Reply...