作为一个有意识的web开发者/网站的所有者,你可能想跟踪您的网站或博客的404页。当然你可以分析你站点的访问日志文件,但我们今天要讲的是一个更好的追踪方式,那就是通过发送邮件报告。这样我么就能更加及时的获取404页面的情况。
如果你的主题没有404.php文件,你必须创建他,然后将一下代码添加到你的404页面中:
注意:使用该方法前提是你的wordpress支持邮件发送。
// 设置页面状态
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,
, $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);
?>