作为一个有意识的 Web 开发者/网站的所有者,你可能想跟踪您的网站或博客的 404 页。当然你可以分析你站点的访问日志文件,但我们今天要讲的是一个更好的追踪方式,那就是通过发送邮件报告。这样我么就能更加及时的获取 404 页面的情况。

<?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 . "nnn";

// 发送邮件
mail($email, "404 Alert: " . $blog . " [" . $theme . "]", $message, "From: $email");

?>