gpt4 book ai didi

wordpress - wp_verify_nonce() 是什么意思?

转载 作者:行者123 更新时间:2023-12-02 04:18:20 24 4
gpt4 key购买 nike

我已阅读reference在 WordPress 上了解了这个函数,但我仍然不明白这个函数到底是做什么的。

我正在阅读有关在 WordPress 中创建元框的教程,并且我在保存数据的函数中包含以下代码:

if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
return $post_id;
}

有人可以简单解释一下 wp_verify_nonce() 的含义吗?

最佳答案

随机数是一个“使用过一次的数字” - WP 用于确保 POST 数据来自安全位置的代码。这对于确保您的插件最终不会消化来自不安全来源的数据很有用(请参阅 Cross-Site Request Forgery )。

This blog post by Mark Jaquith对于理解它们很有用。

[nonces] are unique to the WordPress install, to the WordPress user, to the action, to the object of the action, and to the time of the action (24 hour window). That means that if any of these things changes, the nonce is invalid. So if you (somehow) intercept a nonce being used by me, you would, first of all, only have 24 hours to use this key to attempt to trick me.

要创建随机数,您必须给出 wp_create_nonce某个字符串,为随机数提供“上下文”。它返回一个字符串——随机数本身。然后,您可以将此随机数包含在 POST 请求中。然后,接收页面应该使用相同的上下文创建自己的随机数,并查看它们是否匹配。

在本例中,给出的上下文是 plugin_basename(__FILE__) 。每当从同一个插件中调用它时,这都会生成相同的字符串(请参阅 here )。

当您的 wp_verify_nonce收到在与 Mark 指定的相同情况下创建的随机数,具有相同的上下文字符串,则返回 true。

简而言之:

!wp_verify_nonce

如果 wp_verify_nonce 返回 false,则返回 true。

($_POST[$meta_box['name'].'_noncename'], 

wp_verify_nonce 的第一个参数:要检查的随机数。此代码从 post 请求中获取随机数,存储在 $_POST 全局变量中。

plugin_basename(__FILE__) )

wp_verify_nonce 的第二个参数:生成新随机数的上下文,将根据该新随机数检查第一个随机数。

{ return $post_id; }

如果nonce不匹配,则停止执行当前函数,返回变量$post_id .

关于wordpress - wp_verify_nonce() 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7188518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com