作為一個有意識的Web開發(fā)者/網(wǎng)站的所有者,你可能想跟蹤您的網(wǎng)站或博客的404頁。當(dāng)然你可以分析你站點(diǎn)的訪問日志文件,但我們今天要講的是一個更好的追蹤方式,那就是通過發(fā)送郵件報告。這樣我么就能更加及時的獲取404頁面的情況。
如果你的主題沒有404.php文件,你必須創(chuàng)建他,然后將一下代碼添加到你的404頁面中:
注意:使用該方法前提是你的wordpress支持郵件發(fā)送。
// 設(shè)置頁面狀態(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);
}
// 獲取來源地址
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";
}
// 用戶連接協(xié)議
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";
// 發(fā)送郵件
mail($email, "404 Alert: " . $blog . " [" . $theme . "]", $message, "From: $email");
?>
更多信息請查看IT技術(shù)專欄