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.

Customising the CMS /

RESOLVED: Send notification email to administrator on Error 404 Page


Reply


2 Posts   2409 Views

Avatar
DJMatus23

Community Member, 11 Posts

4 May 2009 at 4:37pm

Edited: 08/05/2009 9:19am

Hi,

I'm about to receive a freshly developed Silverstripe site, unfortunately the developers don't feel it is in the scope of the original project description to have an email sent to the site administrator on every 404 error...so I'm doing it myself, which is fine. My problem is, I can use Debug::send_errors_to('me') to have HTTP 500 sent to me, but not 404s. I don't want to hack the site too much, so I'm hoping there is a nice little directive that means I get a nice little:

404 Error
Source: http://www.whatever.com/FrontPage
Destination: http://www.whatever.com/MissingPage

Can anyone help me, or will I need to hack the published 404 page via .htaccess and PHP (or something similar) to trigger the email? If I need to hack, if anyone has done it before, would appreciate a copy and paste of the .htaccess and PHP bits, to save me some time.

Also, is there a way to change the From address of the error from send_errors_to() other than hacking the Debug.php file?

Cheers
Ronan

P.S. For another 404 question, see: http://silverstripe.org/customising-the-cms/show/259763

Avatar
DJMatus23

Community Member, 11 Posts

8 May 2009 at 9:19am

For anyone who finds themselves looking at this in the future, the solution I ended up with was to edit the following file:

HTTPROOT/themes/THEMENAME/templates/layout/ErrorPage.ss

and add the following text to it (I just put it after the $Content line, but there may be better places for it):

<script>
document.write("\<script type=\"text\/javascript\" src=\"\/error_logging_script.php?err_code="+$ErrorCode+"&from_uri=" + document.referrer + "&to_uri=" + document.URL + "\"\>\<\/script\>");
</script>

Then I created a simple PHP script which took those three GET parameters "err_code", "from_uri" and "to_uri", saved it as error_logging_script.php in the HTTPROOT with the following:

$ip = getenv ("REMOTE_ADDR");

$today = date("D M j Y g:i:s a T");

$message = "Date: $today
IP: $ip
Missing URI: ".$_GET["to_uri"]."
User Agent = $httpagent
Referrer = ".$_GET["from_uri"];

$to = "me@me.com";
$subject = "www.mysite.com Error ".$_GET["err_code"];
$from = "From: webmaster@mysite.com\r\n";

mail($to, $subject, $message, $from);

For brevity, I've taken out all of the additional validation and filtering (to take out bots' failed attempts to hack the site, etc).

It works well (i.e. after 2-3 days use it hasn't missed anything yet), and involves no hacking so it should remain compatible with SS upgrades.

You could also add some logging code, etc., or anything you like really - the reason I put the code just after the $Content line, was because I originally planned to have the PHP print some kind of "Email sent" type text, then realised that was actually a bit silly. I will be adding some code to redirect all 404s back to the front page of the site though, as per my other question - but I'll put that code into the response to that question when I get it done.

Ronan 2.0