MyBB Community Forums

Full Version: Checking for broken links?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys,

I'm trying to create a plugin that will check if a URL exists in a post or not. If it does exist, it will not do anythig. If it doesn't exist, I need to perform a query on it.
Just wondering if anyone knew a good script that checks the URL's on a page and checks if there valid. I've seen a few but most appear to say their valid, even if they don't.

Thanks Smile
Best way to do it IMO is to try visiting the links via CURL to see if they load. Problem is that will be pretty slow unless you do it via a task or something.
Cheers Euan, I found this one:

       //returns true, if domain is availible, false if not
       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;
       }

              if (isDomainAvailible('http://sititmt.com'))
       {
               echo "Up and running!";
       }
       else
       {
               echo "Woops, nothing found there.";
       }

But it is always displaying "Up and running!", even f it doesn't exist (like in the example above)

Cheers Smile