2025年4月10日 星期四 乙巳(蛇)年 正月十一 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 服务器 > 万维网络 > WordPress

WordPress 写会员中心重要的几个安全问题

时间:12-14来源:作者:点击数:13

最近在开发会员中心,总感觉有些安全问题,构造到前台的会员系统也是需要注意很多事情的,有些漏洞可能我们都不能一时间察觉,下面列出一些需要注意的事项。

禁止用户上传特定类型的文件

主题的 functions.php 中插入以下 PHP 代码

  • add_filter('upload_mimes', 'custom_upload_mimes');
  • function custom_upload_mimes( $existing_mimes=array() ) {
  • // 注意中括号中的名称,必须取自上面支持列表中中括号的名称
  • unset( $existing_mimes['exe'] ); //此处禁止了上传exe后缀名的可运行文件
  • unset( $existing_mimes['jpg|jpeg|jpe'] ); //此处禁止了上传jpg、jpeg和jpe后缀名的压缩文件
  • unset( $existing_mimes['gif'] ); //此处禁止了上传gif后缀名的图片文件
  • unset( $existing_mimes['png'] ); //此处禁止了上传png后缀名的图片文件
  • return $existing_mimes;
  • }

只支持上传图片

如何让 WordPress 只支持上传图片文件,其他文件一概拒绝上传。实现方法很简单,我们在当前主题的 functions.php 中插入以下 PHP 代码即可:

  • // Add the filter
  • add_filter('upload_mimes', 'custom_upload_mimes');
  • function custom_upload_mimes( $existing_mimes=array() ) {
  • $existing_mimes = array('jpg|jpeg|jpe' => 'image/jpeg',
  • 'gif' => 'image/gif',
  • 'png' => 'image/png',
  • 'bmp' => 'image/bmp',
  • 'tif|tiff' => 'image/tiff',
  • 'ico' => 'image/x-icon');
  • return $existing_mimes;
  • }

默认角色用户无法进入后台

如果你不想让默认角色的用户进入 WordPress 后台乱逛,你可以在当前主题的 functions.php 中加入以下代码,然后使用默认角色的用户帐号登录

  • if ( is_admin() && ( !defined( 'DOING_AJAX' ) || !DOING_AJAX ) ) {
  • $current_user = wp_get_current_user();
  • if($current_user->roles[0] == get_option('default_role')) {
  • wp_safe_redirect( home_url() );
  • exit();
  • }
  • }

支持用 Email 登录

  • // 修改WordPress用户名过滤机制,通过 Email 获取用户名
  • function ludou_allow_email_login($username, $raw_username, $strict) {
  • if (filter_var($raw_username, FILTER_VALIDATE_EMAIL)) {
  • $user_data = get_user_by('email', $raw_username);
  • if (empty($user_data))
  • wp_die(__('<strong>ERROR</strong>: There is no user registered with that email address.'), '用户名不正确');
  • else
  • return $user_data->user_login;
  • }else {
  • return $username;
  • }
  • }
  • // 修改登录界面的文字,"用户名"改成"用户名或邮箱"
  • function ludou_change_text() {
  • echo '<script type="text/javascript">
  • var user_login_node = document.getElementById("user_login");
  • var old_username_text = user_login_node.parentNode.innerHTML;
  • user_login_node.parentNode.innerHTML = old_username_text.replace(/用户名/, "用户名或邮箱");
  • </script>';
  • }
  • if (in_array($GLOBALS['pagenow'], array('wp-login.php')) &&
  • strpos($_SERVER['REQUEST_URI'], '?action=register') === FALSE &&
  • strpos($_SERVER['REQUEST_URI'], '?action=lostpassword') === FALSE &&
  • strpos($_SERVER['REQUEST_URI'], '?action=rp') === FALSE ) {
  • add_filter('sanitize_user', 'ludou_allow_email_login', 10, 3);
  • add_action('login_footer', 'ludou_change_text');
  • }

支持中文用户名

将以下 PHP 代码复制到当前主题目录下的 functions.php 中,即可让 WordPress 支持使用中文用户名注册和登录:

  • function ludou_non_strict_login( $username, $raw_username, $strict ) {
  • if( !$strict )
  • return $username;
  • return sanitize_user(stripslashes($raw_username), false);
  • }
  • add_filter('sanitize_user', 'ludou_non_strict_login', 10, 3);

注册成功后自动登录

让用户注册成功后自动登录,并跳转到指定页面,即让用户省了手动登录这一步,又提高了用户体验。实现起来很简单,我们可以在当前主题的 functions.php 添加以下 PHP 代码:

  • // 用户注册成功后自动登录,并跳转到指定页面
  • function auto_login_new_user( $user_id ) {
  • wp_set_current_user($user_id);
  • wp_set_auth_cookie($user_id);
  • // 这里设置的是跳转到首页,要换成其他页面
  • // 可以将home_url()改成你指定的URL
  • // 如 wp_redirect( 'http://www.newsky365.com' );
  • wp_redirect( home_url() );
  • exit;
  • }
  • add_action( 'user_register', 'auto_login_new_user' );

退出后跳转到指定页面

这个问题也很好解决,将下面的 PHP 代码放到当前主题的 functions.php 中即可:

  • add_filter('logout_url', 'ludou_logout_redirect', 10, 2);
  • function ludou_logout_redirect($logouturl, $redir) {
  • $redir = 'https://www.cdsy.com.cn/'; // 这里改成你要跳转的网址
  • return $logouturl . '&redirect_to=' . urlencode($redir);
  • }

这样你在后台页面右上角点击退出后,就可以跳转到指定页面了。如果你是想在前台添加一个退出链接,点击后退出登录并跳转到指定站内页面,可以使用以下代码,代码中网址改成你的:

  • <?php if ( $user_ID ) { ?>
  • <a href="<?php echo wp_logout_url( 'https://www.cdsy.com.cn/' ); ?>" title="Logout">Logout</a>
  • <?php } ?>

如果是要跳转到首页,可以使用下面的代码:

  • <?php if ( $user_ID ) { ?>
  • <a href="<?php echo wp_logout_url( home_url() ); ?>" title="Logout">Logout</a>
  • <?php } ?>

如果是要跳转到退出前所在的页面,可以使用以下代码:

  • <?php if ( $user_ID ) { ?>
  • <a href="<?php echo wp_logout_url( home_url(add_query_arg(array(),$wp->request)) ); ?>" title="Logout">Logout</a>
  • <?php } ?>

最后一个,也是一个非常不错的功能

发布新文章 Email 通知用户

很多 WordPress 博客都开放了用户注册的功能,用户可以参与到博客的内容建设当中来,也就是一个博客由多个用户来写。现在有这样的需求,如何实现在某一个用户发表文章后,其他用户都能收到 Email 通知,下面是实现方法:

在当前的 WordPress 主题目录下的 functions.php 中,添加以下 PHP 代码就可以了:

  • function newPostNotify($post_ID) {
  • if( wp_is_post_revision($post_ID) ) return;
  • global $wpdb;
  • $get_post_info = get_post($post_ID);
  • if ( $get_post_info->post_status == 'publish' && $_POST['original_post_status'] != 'publish' ) {
  • // 读数据库,获取所有用户的email
  • $wp_user_email = $wpdb->get_col("SELECT DISTINCT user_email FROM $wpdb->users");
  • // 邮件标题
  • $subject = 'xx博客有新文章';
  • // 邮件内容
  • $message = '文章标题:' . get_the_title($post_ID) . '<br />';
  • $message .= '文章网址:<a href="' . get_permalink($post_ID) . '">' . get_permalink($post_ID) . '</a><br />';
  • // 发邮件
  • $message_headers = "Content-Type: text/html; charset=\"utf-8\"\n";
  • wp_mail($wp_user_email, $subject, $message, $message_headers);
  • }
  • }
  • // 钩子一旦 WordPress 有新文章发布或文章被修改即刻执行newPostNotify函数
  • add_action('publish_post', 'newPostNotify');

有种骚扰的嫌疑,毕竟不是每位注册用户都希望收到类似的邮件。

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐