作为一个有意识的Web开发者/网站的所有者,你可能想跟踪您的网站或博客的404页。当然你可以分析你站点的访问日志文件,但我们今天要讲的是一个更好的追踪方式,那就是通过发送邮件报告。这样我么就能更加及时的获取404页面的情况。
如果你的主题没有404.PHP文件,你必须创建他,然后将一下代码添加到你的404页面中:
注意:使用该方法前提是你的WordPress支持邮件发送,如果不支持可以使用wordpress邮件发送插件:WP Mail SMTP
<?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 (!empty($_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"; } // 来源 URI 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"; } // IP 地址 if (isset($_SERVER['REMOTE_ADDR'])) { $address = clean($_SERVER['REMOTE_ADDR']); } else { $address = "undefined"; } // 用户客户端信息 agent 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("Y-m-d H:i:s", 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 . "\n\n\n"; // 发送邮件 mail($email, "404 Alert: " . $blog . " [" . $theme . "]", $message, "From: $email"); ?>