WordPress站点出现404错误时邮件通知管理员
一个比较好玩的功能就是当我们网站出现了错误时就邮箱通知管理员了,将下面的代码添加到你所用的WordPress主题的 404.php 文件的顶端即可.
如果你的主机支持mail()函数或者设置了SMTP发送邮件,总之就是能够发送邮件就行,一会去查看你设置的WordPress站点的管理员邮箱,你就会发现404通知邮件,可能在垃圾箱哦.
实例代码如下:
- mail($email, "404 Alert: " . $blog . " [" . $theme . "]", $message, "From: $email");
否则我们按下面方法来操作,代码如下:
- <?php
-
-
- header("HTTP/1.1 404 Not Found");
- header("Status: 404 Not Found");
-
-
- $blog = get_bloginfo('name');
- $site = get_bloginfo('url') . '/';
- $email = get_bloginfo('admin_email');
-
-
- if (!emptyempty($_COOKIE["nkthemeswitch" . COOKIEHASH])) {
- $theme = clean($_COOKIE["nkthemeswitch" . COOKIEHASH]);
- } else {
- $theme_data = wp_get_theme();
- $theme = clean($theme_data->Name);
- }
-
-
- if (isset($_SERVER['HTTP_REFERER'])) {
- $referer = clean($_SERVER['HTTP_REFERER']);
- } else {
- $referer = "undefined";
- }
-
- if (isset($_SERVER['REQUEST_URI']) && isset($_SERVER["HTTP_HOST"])) {
- $request = clean('http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
- } else {
- $request = "undefined";
- }
-
- if (isset($_SERVER['QUERY_STRING'])) {
- $string = clean($_SERVER['QUERY_STRING']);
- } else {
- $string = "undefined";
- }
-
- if (isset($_SERVER['REMOTE_ADDR'])) {
- $address = clean($_SERVER['REMOTE_ADDR']);
- } else {
- $address = "undefined";
- }
-
- if (isset($_SERVER['HTTP_USER_AGENT'])) {
- $agent = clean($_SERVER['HTTP_USER_AGENT']);
- } else {
- $agent = "undefined";
- }
-
- if (isset($_SERVER['REMOTE_IDENT'])) {
- $remote = clean($_SERVER['REMOTE_IDENT']);
- } else {
- $remote = "undefined";
- }
-
- $time = clean(date("F jS Y, h:ia", time()));
-
-
- 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");
- ?>