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, Ed, biapar, Willr, Ingo, swaiba

Detecting broken image links


Go to End
Reply


781 Views

Avatar
Bereusei

Community Member, 96 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 = "[email protected]";

$message = "Broken link is detected on the following site: \n
http://www.mysite.com$brokenimageposition";

$headers="From: [email protected]\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: [email protected]\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.