Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the acf domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u378631216/domains/wphai.com/public_html/wp-includes/functions.php on line 6131

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the easy-watermark domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u378631216/domains/wphai.com/public_html/wp-includes/functions.php on line 6131

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the shortcodes-ultimate domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u378631216/domains/wphai.com/public_html/wp-includes/functions.php on line 6131

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the limit-login-attempts-reloaded domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u378631216/domains/wphai.com/public_html/wp-includes/functions.php on line 6131
wordpress防止机器恶意请求的PHP代码示例 - WP大海

在 WordPress 中,防止机器请求(如 CSRF 攻击)的常见方法是使用 Nonce(一次性令牌)。Nonce 是 WordPress 提供的一种安全机制,用于验证请求的来源和合法性。

以下是如何在 WordPress 中使用 Nonce 进行安全请求验证的 PHP 代码示例:

1. 生成 Nonce 并添加到表单中

在表单中添加 Nonce 字段,确保每个表单提交都包含一个唯一的 Nonce。

<form method="post" action="">
    <?php wp_nonce_field('my_action_name', 'my_nonce_field'); ?>
    <input type="text" name="my_field" value="">
    <input type="submit" value="Submit">
</form>

2. 验证 Nonce

在处理表单提交的 PHP 代码中,验证 Nonce 是否有效。

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  // 检查 Nonce 是否存在且有效
  if (!isset($_POST['my_nonce_field']) || !wp_verify_nonce($_POST['my_nonce_field'], 'my_action_name')) {
    // Nonce 验证失败,处理错误
    wp_die('Security check failed');
  }

  // Nonce 验证成功,继续处理表单数据
  $my_field_value = sanitize_text_field($_POST['my_field']);
  // 处理表单数据...
}

3. 在 AJAX 请求中使用 Nonce

如果你使用 AJAX 请求,也可以在 AJAX 请求中添加 Nonce。

前端 JavaScript

jQuery(document).ready(function($) {
  var data = {
    action: 'my_ajax_action',
    my_nonce: '<?php echo wp_create_nonce('my_ajax_nonce'); ?>',
    my_field: 'some_value'
  };

  $.post('<?php echo admin_url('admin-ajax.php'); ?>', data, function(response) {
    console.log(response);
  });
});

后端 PHP

add_action('wp_ajax_my_ajax_action', 'my_ajax_handler');
add_action('wp_ajax_nopriv_my_ajax_action', 'my_ajax_handler');

function my_ajax_handler() {
  // 检查 Nonce 是否存在且有效
  if (!isset($_POST['my_nonce']) || !wp_verify_nonce($_POST['my_nonce'], 'my_ajax_nonce')) {
    wp_send_json_error('Security check failed');
  }

  // Nonce 验证成功,继续处理 AJAX 请求
  $my_field_value = sanitize_text_field($_POST['my_field']);
  // 处理数据...

  wp_send_json_success('Request processed successfully');
}

4. 其他安全措施

除了 Nonce 验证,你还可以考虑以下安全措施:

  • 限制请求频率:使用插件或自定义代码限制每个用户的请求频率。
  • 验证用户权限:使用 current_user_can() 函数检查用户是否有权限执行操作。
  • 使用 HTTPS:确保网站使用 HTTPS 加密传输数据。
  • 输入验证和过滤:使用 sanitize_text_field()esc_url() 等函数对用户输入进行过滤和验证。

通过这些方法,你可以有效地防止机器请求和其他安全威胁。

 

常见问题
如何下载?
可以点击右侧边栏或者文章底部的【立即下载】按钮。然后按照【网盘地址】进行下载。
不是最新版本?
可以提交工单。或者发邮件到 uishop@qq.com。
声明:1、本站大部分资源均为网络采集所得,仅供用来学习研究,请于下载后的24h内自行删除,正式商用请购买正版。2、所有汉化类文件和个别标注了“原创”的产品均为本站原创发布,任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。3、如若本站内容侵犯了原著者的合法权益,请携带相关版权文件联系我们进行下架或删除。4、虚拟下载类资源具有可复制性,一经下载后本站有权拒绝退款或更换其他商品!
0

评论0

没有账号?注册  忘记密码?

社交账号快速登录