views:

239

answers:

4

Hey! I got this link checker script and i would like it to give me a mail when a link doesnt work. I need it to remember that it send me an email about a link so i dont get multiple emails about the same link.

I would appeciate if anyone help me with this since it was too hard for me.

<?
function checklink($links) {
    $working = 0;
    $link = $links;
    $links = preg_replace('/\s+/', '', $links);

    if(strstr($links,"rapidshare.com")){
     $var = file_get_contents($links);
     if(strpos($var,"You want to download the file ")) {
     $working = 1;
     }
    }
    elseif (strstr($links,"megaupload.com")) {
     $var1 = file_get_contents($links);
     if(strpos($var1,"Please enter")) {
     $working = 1;
     }
    }
    elseif (strstr($links,"megashares.com")) {
      $var2 = file_get_contents($links);
      if(strpos($var2,"Filename:")) {
      $working = 1;
      }
    }
    elseif (strstr($links,"sendspace.com")) {
     $var3 = file_get_contents($links);
     if(strpos($var3,"404 Page Not Found")) {
      $working = 0;
     }
     elseif(strpos($var3,"Sorry, the file you requested is not available.")){
      $working = 0;
     }
     else {
      $working = 1;
     }
     }
    elseif(strstr($links,"rapidshare.de")) {
     $var5 = file_get_contents($links);
     if(strpos($var,"You want to download the file ")){
      $working = 1;
     }
    }
    elseif (strstr($links,"mediafire.com")) {
     $var4 = file_get_contents($links);
     if(strpos($var4,"Sharing")) {
     $working = 1;
     }
    }

    if ($working == 1) {
     echo "<a href=\"". $link . "\" target=\"_blank\">". $link . "</a>";
    }
    else {
     echo "The link is not working. Please let me know about it and I'll fix it.";
    }
}
?>
+1  A: 

I think the best way would be to collect the links and store them in a database table.

Then have a system that goes through the links and checks, if it works it marks it as a working link and if it doesn't then it marks it as a broken link and sends you an email.

You would then have to do a check to see if a link is in a database (since you cant use mysql's varchar as a unique, because it's maxed out at 255 and links can be longer)

If it is in the database, then check what the result of the scan was.

BTW your way of using file_get_contents is a slow process. Since it downloads the entire page. I would recommend using cURL.

Ólafur Waage
Would you mind helping my some of that?
+1  A: 

I agree with Olafur, but alternatively if you haven't got access to a database you could use the server's file system to save the stats of an URL in a combined config/log file, like a comma-delimited file. Let's say you have a file like this:

rapidshare.com,You want to download the file,0,0
megaupload.com,Please enter,0,0
megashares.com,Filename:,0,0

The four fields are 'URL', 'text to check for', 'last check result' and 'has mail been sent'. The code could be something like this:

$file = "myfile.txt";

// open the file
$fh = fopen($filename, "r");

// read the full file contents into a string
$contents = fread($fh, filesize($file));

// close the file
fclose($fh);

// split the string into an array of lines
$lines = split("\n", $contents);

// split each line into its fields for processing
$i = 0;
foreach ($lines as $line) {
   $checkarray[$i] = split(",", $line);
   $i++;
}

Now you can cycle through the array and do whatever you want, writing back the information including the 'mail sent' status field as you go along. Use $fields[0] for the URL, $fields[1] for the text to check for, and you can read the last status with $fields[2] en the 'mail sent' status with $fields[3].

foreach($checkarray as $fields) {
   // insert code to do your checks here
   ...

   // write back the results
   $fh = fopen($filename, "w");
   fwrite($fh, $fields[0] . "," . $fields[1] . "," . $working . "," . $mailsent . "\n";
   fclose($fh);

}

Hope this helps you on your way.

kathmann
A: 

Hi, this is the code you want:

function StatusCheck($url)
{
$urlparts=parse_url($url);
$curl=new CCurl($url);
$headers=$curl->execute();
$headers=$curl->close();
$headers=$curl->getHeader();
$headers=split("\r\n",$headers);
$status=$headers[0];
print_r($headers);
if (strpos($status,"HTTP/1.1 200 OK")===FALSE)
   {
   echo date("d.m.Y H:i:s").$url,': bad'."\n";
   return 0;
   }
else
   {
   echo date("d.m.Y H:i:s").$url,': good'."\n";
   return 1;
   }
}

it checks the URL (link) provided and prints out the headers + info if the URL is bad (not working) or good (status 200 OK)

PS: set curl options to follow redirection

EDIT: this is the CCurl class, sorry forgot about it:

class CCurl {
   var $m_handle;
   var $m_header;
   var $m_body;

   function CCurl($sUrl) {
       $this->m_handle = curl_init();
       curl_setopt($this->m_handle, CURLOPT_URL, $sUrl);
       curl_setopt($this->m_handle, CURLOPT_HEADER, 1);
       curl_setopt($this->m_handle, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt($this->m_handle, CURLOPT_FOLLOWLOCATION, 1);
       curl_setopt($this->m_handle, CURLOPT_USERAGENT, "StatusCheckBot 0.1");
       return;
   }

   function getHeader() {
       return $this->m_header;
   }

   function execute() {
       $sResponse = curl_exec($this->m_handle);
       $this->m_body = substr($sResponse, strpos($sResponse, "\r\n\r\n") + 4);
       $this->m_header = substr($sResponse, 0, -strlen($this->m_body));
       return $this->m_body;
   }

   function close() {
       curl_close($this->m_handle);
       return;
   }
}
dusoft
I got this errorFatal error: Class 'CCurl' not found in /home/pema2201/public_html/downloads/checklinks.php on line 5http://anitard.org/downloads/hack_legend_of_the_twilight_bracelet.php
A: 

I already got answer to this post before i posted it.