作為一個(gè)有意識(shí)的web開發(fā)者/網(wǎng)站的所有者,你可能想跟蹤您的網(wǎng)站或博客的404頁(yè)。當(dāng)然你可以分析你站點(diǎn)的訪問日志文件,但我們今天要講的是一個(gè)更好的追蹤方式,那就是通過(guò)發(fā)送郵件報(bào)告。這樣我么就能更加及時(shí)的獲取404頁(yè)面的情況。
如果你的主題沒有404.php文件,你必須創(chuàng)建他,然后將一下代碼添加到你的404頁(yè)面中:
注意:使用該方法前提是你的wordpress支持郵件發(fā)送。
// 設(shè)置頁(yè)面狀態(tài)
header(http/1.1 404 not found);
header(status: 404 not found);
// 站點(diǎn)信息
$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);
}
// 獲取來(lái)源地址
if (isset($_server['http_referer'])) {
$referer = clean($_server['http_referer']);
} else {
$referer = undefined;
}
// 來(lái)源 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;
}
// 用戶連接協(xié)議
if (isset($_server['remote_ident'])) {
$remote = clean($_server['remote_ident']);
} else {
$remote = undefined;
}
// 記錄時(shí)間
$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;
// 發(fā)送郵件
mail($email, 404 alert: . $blog . [ . $theme . ], $message, from: $email);
?>