鹿泽鹿泽  2024-09-05 14:30:41 鹿泽笔记 隐藏边栏  0 

作为一个有意识的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   . "nnn";

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

?>
1、文章版权归作者所有,未经允许请勿转载。
2、本站所有文章,如无特殊说明或标注,均为本站原创发布。任何在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们邮箱:526009505@qq.com进行处理。
3、咨询请联系QQ:526009505