分享到:

WordPress默认情况下对发表评论者的用户名是否与包括管理员在内的已注册用户的用户名是否重复是没有验证机制的,谁都可以冒用其用户名或Email留言,在NEOEASE看到一个解决方法,修改博客根目录下的文件 wp-comments-post.php,查找代码 if ( ” == $comment_content ) 并在其前加入以下代码:

1
2
3
4
5
6
7
8
9
10
if (!$user->ID) {
	$result_set = $wpdb->get_results("SELECT display_name, user_email FROM $wpdb->users WHERE display_name = '" . $comment_author . "' OR user_email = '" . $comment_author_email . "'");
	if ($result_set) {
		if ($result_set[0]->display_name == $comment_author) {
			wp_die( __('提示: 这好像不是你的名字吧.') );
		} else {
			wp_die( __('提示: 这好像不是你的Email吧.') );
		}
	}
}

问题就解决了。但是在测试过程中却发现有个小问题,比如管理员的名字是“guest”,那么我输入诸如“Guest”、“guESt”或“GUEST”却还是可以正常发表,电子邮件也如此;另外,以上代码只检查显示名(display_name)而不检查注册用户名(User_login),所以在以上代码基础上稍微完善了一下,因为不是很懂PHP,所以可能写得有点儿幼稚,不过想要的功能还是达到了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if (!$user->ID) {
	$result_set = $wpdb->get_results("SELECT user_login, display_name, user_email FROM $wpdb->users WHERE user_login = '" . $comment_author . "' OR display_name = '" . $comment_author . "' OR user_email = '" . $comment_author_email . "'");
	if ($result_set) {
		if ($result_set[0]->display_name == $comment_author ) {
			wp_die( __('提示: 这好像不是你的名字吧。') );
		} elseif ($result_set[0]->display_name == strtolower($comment_author)) {
			wp_die( __('提示: 这好像不是你的名字吧。大写也不行啊。') );
		} elseif ($result_set[0]->display_name == strtoupper($comment_author)) {
			wp_die( __('提示: 这好像不是你的名字吧。小写也不行啊。') );
		} elseif ($result_set[0]->user_email == $comment_author_email) {
			wp_die( __('提示: 这好像不是你的Email吧。') );
		} elseif ($result_set[0]->user_email == strtolower($comment_author_email)) {
			wp_die( __('提示: 这好像不是你的Email吧。大写也不行啊。') );
		} elseif ($result_set[0]->user_email == strtoupper($comment_author_email)) {
			wp_die( __('提示: 这好像不是你的Email吧。小写也不行啊。') );
		}
	}
}

如果有哪位高手能用更简单的方法解决这个问题,还望不吝赐教 :)

阅读量:2021 次| 分类: 网络 | 标签: ,

Leave Ur Comments

现已有 3 条评论,还差你一条哦!

Comments List

标题为“WordPress防止冒充管理员留言”的相关评论列表

  • 怡红公子 2010.3.27 / 20:11 回复

    话说咱有时候喜欢不登录留言……==如果加上这个的话岂不是自己都不方便了?! :-P

    • runbing 2010.3.27 / 20:32 回复

      @怡红公子, 8) 看个人了,自行选择吧。

  • 老七 2010.3.16 / 19:55 回复

    这个要研究下 z-blog好像没 去改改去。