Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Detecting broken image links


Reply


592 Views

Avatar
Bereusei

Community Member, 94 Posts

16 May 2012 at 7:33pm

Edited: 16/05/2012 7:34pm

Hey guys,

I´ve a silverstripe-website with hundreds of images. Anyway, sometimes the links to images gets broken (no idea why). The result is, that users see a white paper thing instead of the image. But I´ve created a little jquery-php solution:

for the javascript that appears on every site:

$check = false;
   
   $("img").error(function(){
   $currentUrl = window.location.pathname;
   if(!$check){
      $.post("themes/mytheme/php/send.php", { brokenimage: $currentUrl } );
         $check = true;
      }
   });

for the send.php:
<?php
   $brokenimageposition = $_POST['brokenimage'];
   $brokenimageposition = stripcslashes($brokenimageposition);
   $brokenimageposition = htmlspecialchars($brokenimageposition);
   
   ##Send activation Email
         
   $subject = "Broken image link";

   $to = "your@email.com";
         
   $message = "Broken link is detected on the following site: \n
            http://www.mysite.com$brokenimageposition";
         
   $headers="From: error@mysite.com\n";
   $headers.="MIME-Version: 1.0\n";
   $headers.="Content-Type: text/plain;\n\t charset=\"utf-8\"\n";
   $headers.="Content-Transfer-Encoding: 8bit\n";
   $headers.="X-Sender: error@mysite.com\n";          
   $headers.="X-Mailer: PHP\n";
   $headers.="X-Priority: 3\n";
   
mail($to, $subject, $message, $headers);
         
?>

This should work for any other 40X-error as well. I hope it is useful for someone.