5099 Posts in 1519 Topics by 1116 members
|Go to End|
4 May 2009 at 4:37pm Last edited: 8 May 2009 9:19am
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:
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?
P.S. For another 404 question, see: http://silverstripe.org/customising-the-cms/show/259763
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:
and add the following text to it (I just put it after the $Content line, but there may be better places for it):
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
Missing URI: ".$_GET["to_uri"]."
User Agent = $httpagent
Referrer = ".$_GET["from_uri"];
$to = "firstname.lastname@example.org";
$subject = "www.mysite.com Error ".$_GET["err_code"];
$from = "From: email@example.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.
|Go to Top|