404.php
file:
[php]
// WP 404 ALERTS @ http://wp-mix.com/wordpress-404-email-alerts/
// set status
header("HTTP/1.1 404 Not Found");
header("Status: 404 Not Found");
// site info
$blog = get_bloginfo('name');
$site = get_bloginfo('url') . '/';
$email = get_bloginfo('admin_email');
// theme info
if (!empty($_COOKIE["nkthemeswitch" . COOKIEHASH])) {
$theme = clean($_COOKIE["nkthemeswitch" . COOKIEHASH]);
} else {
$theme_data = wp_get_theme();
$theme = clean($theme_data->Name);
}
// referrer
if (isset($_SERVER['HTTP_REFERER'])) {
$referer = clean($_SERVER['HTTP_REFERER']);
} else {
$referer = "undefined";
}
// request URI
if (isset($_SERVER['REQUEST_URI']) && isset($_SERVER["HTTP_HOST"])) {
$request = clean('http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
} else {
$request = "undefined";
}
// query string
if (isset($_SERVER['QUERY_STRING'])) {
$string = clean($_SERVER['QUERY_STRING']);
} else {
$string = "undefined";
}
// IP address
if (isset($_SERVER['REMOTE_ADDR'])) {
$address = clean($_SERVER['REMOTE_ADDR']);
} else {
$address = "undefined";
}
// user agent
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$agent = clean($_SERVER['HTTP_USER_AGENT']);
} else {
$agent = "undefined";
}
// identity
if (isset($_SERVER['REMOTE_IDENT'])) {
$remote = clean($_SERVER['REMOTE_IDENT']);
} else {
$remote = "undefined";
}
// log time
$time = clean(date("F jS Y, h:ia", time()));
// sanitize
function clean($string) {
$string = rtrim($string);
$string = ltrim($string);
$string = htmlentities($string, ENT_QUOTES);
$string = str_replace("n", "<br>", $string);
if (get_magic_quotes_gpc()) {
$string = stripslashes($string);
}
return $string;
}
$message =
"TIME: " . $time . "n" .
"*404: " . $request . "n" .
"SITE: " . $site . "n" .
"THEME: " . $theme . "n" .
"REFERRER: " . $referer . "n" .
"QUERY STRING: " . $string . "n" .
"REMOTE ADDRESS: " . $address . "n" .
"REMOTE IDENTITY: " . $remote . "n" .
"USER AGENT: " . $agent . "nnn";
mail($email, "404 Alert: " . $blog . " [" . $theme . "]", $message, "From: $email");
[/php]
No editing required, just include and done. To disable the email reporting, either remove the script or comment-out the mail()
function, like so:
[php]// mail($email, "404 Alert: " . $blog . " [" . $theme . "]", $message, "From: $email");[/php]
Note that any of the variables (e.g., $time
, $request
, et al) are available for use elsewhere on the page. For example, you could display the information to the visitor to help deliver a useful 404 page.
Here is a screenshot showing how the email alerts will look in your inbox.
Update! Thanks to Drew Jaynes for sharing his OOP-enhanced version of the 404-email script. Muy bueno!!Revisions
- September 2, 2015 @ 14:25:47 [Current Revision] by PeterLugg
- September 2, 2015 @ 14:25:47 by PeterLugg
- August 15, 2013 @ 03:16:10 by PeterLugg
- August 15, 2013 @ 03:15:46 by PeterLugg
Revision Differences
August 15, 2013 @ 03:15:46 | Current Revision | ||
---|---|---|---|
Content | |||
Added: As seen on WP Mix: <a href="http:// wp-mix.com/wordpress-404- email-alerts/ ">http://wp- mix.com/wordpress- 404-email-alerts/</a> | |||
Unchanged: Here is a customized plug-n-play <abbr title="PHP: Hypertext Preprocessor" >PHP</abbr> script for reporting 404 errors via email. No editing required, just clean email alerts for each 404 error. | Unchanged: Here is a customized plug-n-play <abbr title="PHP: Hypertext Preprocessor" >PHP</abbr> script for reporting 404 errors via email. No editing required, just clean email alerts for each 404 error. | ||
Unchanged: To implement, simply include this script at the top of your theme’s <code> 404.php</code> file: | Unchanged: To implement, simply include this script at the top of your theme’s <code> 404.php</code> file: | ||
Unchanged: [php] | Unchanged: [php] | ||
Unchanged: // WP 404 ALERTS @ http://wp-mix.com/ wordpress-404- email-alerts/ | Unchanged: // WP 404 ALERTS @ http://wp-mix.com/ wordpress-404- email-alerts/ | ||
Unchanged: // set status | Unchanged: // set status | ||
Unchanged: header("HTTP/1.1 404 Not Found"); | Unchanged: header("HTTP/1.1 404 Not Found"); | ||
Unchanged: header("Status: 404 Not Found"); | Unchanged: header("Status: 404 Not Found"); | ||
Unchanged: // site info | Unchanged: // site info | ||
Unchanged: $blog = get_bloginfo('name'); | Unchanged: $blog = get_bloginfo('name'); | ||
Unchanged: $site = get_bloginfo('url') . '/'; | Unchanged: $site = get_bloginfo('url') . '/'; | ||
Unchanged: $email = get_bloginfo( 'admin_email'); | Unchanged: $email = get_bloginfo( 'admin_email'); | ||
Unchanged: // theme info | Unchanged: // theme info | ||
Unchanged: if (!empty($_COOKIE[" nkthemeswitch" . COOKIEHASH])) { | Unchanged: if (!empty($_COOKIE[" nkthemeswitch" . COOKIEHASH])) { | ||
Unchanged: $theme = clean($_COOKIE[" nkthemeswitch" . COOKIEHASH]); | Unchanged: $theme = clean($_COOKIE[" nkthemeswitch" . COOKIEHASH]); | ||
Unchanged: } else { | Unchanged: } else { | ||
Unchanged: $theme_data = wp_get_theme(); | Unchanged: $theme_data = wp_get_theme(); | ||
Unchanged: $theme = clean($theme_ data->Name); | Unchanged: $theme = clean($theme_ data->Name); | ||
Unchanged: } | Unchanged: } | ||
Unchanged: // referrer | Unchanged: // referrer | ||
Unchanged: if (isset($_SERVER['HTTP_REFERER'])) { | Unchanged: if (isset($_SERVER['HTTP_REFERER'])) { | ||
Unchanged: $referer = clean($_SERVER['HTTP_REFERER']); | Unchanged: $referer = clean($_SERVER['HTTP_REFERER']); | ||
Unchanged: } else { | Unchanged: } else { | ||
Unchanged: $referer = "undefined"; | Unchanged: $referer = "undefined"; | ||
Unchanged: } | Unchanged: } | ||
Unchanged: // request URI | Unchanged: // request URI | ||
Unchanged: if (isset($_SERVER['REQUEST_URI']) && isset($_SERVER[" HTTP_HOST"])) { | Unchanged: if (isset($_SERVER['REQUEST_URI']) && isset($_SERVER[" HTTP_HOST"])) { | ||
Unchanged: $request = clean('http://' . $_SERVER[" HTTP_HOST"] . $_SERVER[" REQUEST_URI"]); | Unchanged: $request = clean('http://' . $_SERVER[" HTTP_HOST"] . $_SERVER[" REQUEST_URI"]); | ||
Unchanged: } else { | Unchanged: } else { | ||
Unchanged: $request = "undefined"; | Unchanged: $request = "undefined"; | ||
Unchanged: } | Unchanged: } | ||
Unchanged: // query string | Unchanged: // query string | ||
Unchanged: if (isset($_SERVER['QUERY_STRING'])) { | Unchanged: if (isset($_SERVER['QUERY_STRING'])) { | ||
Unchanged: $string = clean($_SERVER['QUERY_STRING']); | Unchanged: $string = clean($_SERVER['QUERY_STRING']); | ||
Unchanged: } else { | Unchanged: } else { | ||
Unchanged: $string = "undefined"; | Unchanged: $string = "undefined"; | ||
Unchanged: } | Unchanged: } | ||
Unchanged: // IP address | Unchanged: // IP address | ||
Unchanged: if (isset($_SERVER['REMOTE_ADDR'])) { | Unchanged: if (isset($_SERVER['REMOTE_ADDR'])) { | ||
Unchanged: $address = clean($_SERVER['REMOTE_ADDR']); | Unchanged: $address = clean($_SERVER['REMOTE_ADDR']); | ||
Unchanged: } else { | Unchanged: } else { | ||
Unchanged: $address = "undefined"; | Unchanged: $address = "undefined"; | ||
Unchanged: } | Unchanged: } | ||
Unchanged: // user agent | Unchanged: // user agent | ||
Unchanged: if (isset($_SERVER['HTTP_ USER_AGENT'])) { | Unchanged: if (isset($_SERVER['HTTP_ USER_AGENT'])) { | ||
Unchanged: $agent = clean($_SERVER['HTTP_ USER_AGENT']); | Unchanged: $agent = clean($_SERVER['HTTP_ USER_AGENT']); | ||
Unchanged: } else { | Unchanged: } else { | ||
Unchanged: $agent = "undefined"; | Unchanged: $agent = "undefined"; | ||
Unchanged: } | Unchanged: } | ||
Unchanged: // identity | Unchanged: // identity | ||
Unchanged: if (isset($_SERVER['REMOTE_IDENT'])) { | Unchanged: if (isset($_SERVER['REMOTE_IDENT'])) { | ||
Unchanged: $remote = clean($_SERVER['REMOTE_IDENT']); | Unchanged: $remote = clean($_SERVER['REMOTE_IDENT']); | ||
Unchanged: } else { | Unchanged: } else { | ||
Unchanged: $remote = "undefined"; | Unchanged: $remote = "undefined"; | ||
Unchanged: } | Unchanged: } | ||
Unchanged: // log time | Unchanged: // log time | ||
Unchanged: $time = clean(date("F jS Y, h:ia", time())); | Unchanged: $time = clean(date("F jS Y, h:ia", time())); | ||
Unchanged: // sanitize | Unchanged: // sanitize | ||
Unchanged: function clean($string) { | Unchanged: function clean($string) { | ||
Unchanged: $string = rtrim($string); | Unchanged: $string = rtrim($string); | ||
Unchanged: $string = ltrim($string); | Unchanged: $string = ltrim($string); | ||
Unchanged: $string = htmlentities($string, ENT_QUOTES); | Unchanged: $string = htmlentities($string, ENT_QUOTES); | ||
Deleted: $string = str_replace(" | Added: $string = str_replace("n", "<br>", $string); | ||
Unchanged: if (get_magic_quotes_gpc()) { | Unchanged: if (get_magic_quotes_gpc()) { | ||
Unchanged: $string = stripslashes($string); | Unchanged: $string = stripslashes($string); | ||
Unchanged: } | Unchanged: } | ||
Unchanged: return $string; | Unchanged: return $string; | ||
Unchanged: } | Unchanged: } | ||
Unchanged: $message = | Unchanged: $message = | ||
Deleted: "TIME: " . $time . " | Added: "TIME: " . $time . "n" . | ||
Deleted: "*404: " . $request . " | Added: "*404: " . $request . "n" . | ||
Deleted: "SITE: " . $site . " | Added: "SITE: " . $site . "n" . | ||
Deleted: "THEME: " . $theme . " | Added: "THEME: " . $theme . "n" . | ||
Deleted: "REFERRER: " . $referer . " | Added: "REFERRER: " . $referer . "n" . | ||
Deleted: "QUERY STRING: " . $string . " | Added: "QUERY STRING: " . $string . "n" . | ||
Deleted: "REMOTE ADDRESS: " . $address . " | Added: "REMOTE ADDRESS: " . $address . "n" . | ||
Deleted: "REMOTE IDENTITY: " . $remote . " | Added: "REMOTE IDENTITY: " . $remote . "n" . | ||
Deleted: "USER AGENT: " . $agent . " | Added: "USER AGENT: " . $agent . "nnn"; | ||
Unchanged: mail($email, "404 Alert: " . $blog . " [" . $theme . "]", $message, "From: $email"); | Unchanged: mail($email, "404 Alert: " . $blog . " [" . $theme . "]", $message, "From: $email"); | ||
Unchanged: [/php] | Unchanged: [/php] | ||
Unchanged: No editing required, just include and done. To disable the email reporting, either remove the script or comment-out the <code>mail( )</code> function, like so: | Unchanged: No editing required, just include and done. To disable the email reporting, either remove the script or comment-out the <code>mail( )</code> function, like so: | ||
Unchanged: [php]// mail($email, "404 Alert: " . $blog . " [" . $theme . "]", $message, "From: $email");[/php] | Unchanged: [php]// mail($email, "404 Alert: " . $blog . " [" . $theme . "]", $message, "From: $email");[/php] | ||
Unchanged: Note that any of the variables (e.g., <code> $time</code>, < code>$request</code>, et al) are available for use elsewhere on the page. For example, you could display the information to the visitor to help deliver a useful 404 page. | Unchanged: Note that any of the variables (e.g., <code> $time</code>, < code>$request</code>, et al) are available for use elsewhere on the page. For example, you could display the information to the visitor to help deliver a useful 404 page. | ||
Deleted: <a href="http:// | Added: <a href="http:// wiki.pixelpress.com.au/files/ 2013/08/email- 404-alert.gif">Here is a screenshot</a> showing how the email alerts will look in your inbox. | ||
Unchanged: Update! Thanks to <a href="https:/ /twitter.com/ DrewAPicture/ status/306120533202837505">Drew Jaynes</a> for sharing his <a href="https:/ /gist.github.com/ DrewAPicture/ 5032207">OOP-enhanced version of the 404-email script</a>. Muy bueno!! | Unchanged: Update! Thanks to <a href="https:/ /twitter.com/ DrewAPicture/ status/306120533202837505">Drew Jaynes</a> for sharing his <a href="https:/ /gist.github.com/ DrewAPicture/ 5032207">OOP-enhanced version of the 404-email script</a>. Muy bueno!! |
Note: Spaces may be added to comparison text to allow better line wrapping.
No comments yet.