- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经构建了一个自定义的 WooCommerce 购物车,您可以在其中使用 AJAX 更改数量和/或从购物车中删除项目,但它有问题,我认为这与 WooCommerce 和 WordPress nonces 的有效性有关。
问题:
当您的购物车中有产品并且您至少刷新页面一次时 - 将产品添加到您的购物车后,它会起作用。
当您第一次访问时,它不起作用,您将产品添加到购物车中并尝试编辑产品的数量,或尝试删除它。
亲自查看 https://staging.noten.nl/noten/ - 请检查您的智能手机。将产品添加到购物车,单击它并更改值(多 250 克/少 250 克/删除产品)
PHP - Nonce 创建
/*
** Theme scripts
*/
function scripts() {
// Enqueue scripts
wp_enqueue_script( 'noten-nl/js', Assets\asset_path('scripts/main.js?v=' . VERSION), ['jquery-core', 'wp-util'], null, true );
// Localize script
wp_localize_script( 'noten-nl/js', 'shop', array(
'url' => admin_url( 'admin-ajax.php' ),
'cart_more' => wp_create_nonce( 'cart-more-nonce' ),
'cart_less' => wp_create_nonce( 'cart-less-nonce' ),
'cart_delete' => wp_create_nonce( 'cart-delete-nonce' )
));
}
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\\scripts', 999 );
/*
** Edit items in cart
*/
function cartAction(event) {
// Log
console.log('cartAction');
// Variables
var action = $(event.currentTarget).attr('data-action'),
product = $('.cart-products-scroll .row.active');
// Load
product.children('.cart-row-item-loading').show();
// AJAX
wp.ajax.send('cart_' + action, {
data: {
nonce: shop['cart_' + action],
id: product.attr('data-product-id'),
quantity: product.attr('data-product-quantity'),
key: product.attr('data-product-cart-item-key')
},
success: function (fragments) {
// Replace fragments
$.each(fragments, function (key, value) {
$(key).replaceWith(value);
});
},
error: function (response) {
console.log(response);
}
});
}
function cart_more() {
// Log
write_log( 'cart_more()' );
// Variables
$nonce = isset( $_POST['nonce'] ) ? $_POST['nonce'] : '';
$product_id = isset( $_POST['id'] ) ? $_POST['id'] : '';
$product_quantity = isset( $_POST['quantity'] ) ? $_POST['quantity'] : '';
// Check data
if ( wp_verify_nonce( $nonce, 'cart-more-nonce' ) && ! empty( $product_id ) && ! empty( $product_quantity ) ) {
/*
** Removed for readability
*/
// Send success
wp_send_json_success( $fragments );
} else {
// Send error
wp_send_json_error( ':\'(' );
}
}
add_action( 'wp_ajax_nopriv_cart_more', __NAMESPACE__ . '\\cart_more' );
add_action( 'wp_ajax_cart_more', __NAMESPACE__ . '\\cart_more' );
最佳答案
为了使产品页面可缓存,在创建购物车之前不会创建 WooCommerce session 。1,2
WooCommerce 使用一个值覆盖 nonce 参数之一,该值根据是否已创建 WooCommerce session 而改变。3,4
当您为没有购物车和 session 的新用户创建随机数时,随机数是使用一组输入计算的。当您在将商品添加到购物车后检查 nonce 时,会使用一组不同的输入生成检查值,因为 WooCommerce session 现在存在。这会导致生成不同的 nonce 值,并且针对旧的 nonce 值的 nonce 检查失败。
一种解决方法是在创建 nonce 之前主动创建 WooCommerce session 。请注意,这可能会影响您网站的缓存方式。
关于WordPress nonce 未验证 WooCommerce 购物车操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42325697/
几年前,我在 stackoverflow 上询问过如何使 PHP 密码存储安全。main answer建议使用以下哈希算法: function hash_password($password, $no
当我创建自己的模板时,我了解使用随机数的过程。 但是我正在开发一个仅使用 Wordpress REST API 来提取数据的 ReactJS 应用程序,因此用户永远不会访问 index.php,而是对
我正在尝试为 Intranet(基于 Orchard CMS)对 Azure AD 和 Graph 进行身份验证,这在我的本地计算机上按预期运行,但是,当访问将成为生产站点的内容时(已经在 ssl 上
到目前为止,当使用 API 进行身份验证时,我已经使用它来生成一个简单的 nonce: def nonce(): return str(int(time.time()) * 1000) It
我在尝试向服务器发送 soap 请求 (soapCall) 时遇到错误。 Fatal error: Uncaught SoapFault exception: [ns1:InvalidSecurity
我的设置是: Laravel 4.2 Braintreepayments JS + PHP Laravel Braintree 我试图添加一个新客户 as showed in the tutorial
我正在尝试将 nonce 值添加到我的内联脚本中以满足更严格的 CSP。但是,我遇到了一个奇怪的问题,即 chrome 正在从 nonce 属性中剥离值。当我 curl 页面时,会出现 nonce 值
我正在服务器端编写 OAuth 协议(protocol)的提供者部分,并且我正在研究我需要缓存的 OAuth 消费者发送的随机数。 根据推特的文档, Twitter will only allow a
我使用 web3 和供应商主网。我按契约(Contract)进行了 2 笔交易。首先是批准方法,另一笔交易是多次转账。我将第二个签名存储在数据库中。如果第一笔交易成功,我发送第二笔交易/。第二个事务几
我在我的 nuxtjs 项目中使用了 nuxt-auth。 enter image description here 最佳答案 您需要将responseType指定为“token id_token”:
我在通过 ajax 请求进行基本随机数验证时遇到问题。 这些是我的脚本加载器和 css 加载器函数: (在gallery.php中) function gallery_js_loader() {
我在我的 nuxtjs 项目中使用了 nuxt-auth。 enter image description here 最佳答案 您需要将responseType指定为“token id_token”:
我正在尝试向 Square 提交付款,但不确定 card_nonce 代表什么。 在此处找到的完整 API 文档:https://docs.connect.squareup.com/api/conne
嗨,有人可以帮助我在调用 outlook rest api 时遇到以下错误吗 IDX21323:RequireNonce 是“[PII 默认隐藏。将 IdentityModelEventSource.
我对随机数的理解很模糊,但有点困惑。 nonce 验证失败时的正确响应是什么? 在什么情况下 nonce 验证会失败?对真正的用户有什么风险? 最佳答案 表单随机数的目标通常有两个:确保数据只提交一次
我不确定我是否理解正确:消息计数器可以用作/代替 nonce? 我的意思是这样的消息: Header(2bytes) | counter(8bytes) | Body(n bytes encrypte
我正在开发一种软件,其中使用带有 Oauth2 的 Microsoft Azure AD 完成登录过程。随机数在此过程中是可选的,成功登录后我会收到一个有效期为一小时的 token 。 我无法理解
除了随机的 30 个字符的字母数字字符串作为我的 nonce 之外,我真的可以让我的请求更安全吗? 最佳答案 实际上,尽管 RFC 说“随机字符串”,但随机数不一定是随机的。它必须是独一无二的。 使用
我已经构建了一个自定义的 WooCommerce 购物车,您可以在其中使用 AJAX 更改数量和/或从购物车中删除项目,但它有问题,我认为这与 WooCommerce 和 WordPress nonc
这个问题here是关于创建一个身份验证方案。 AviD 给出的接受答案指出 Your use of a cryptographic nonce is also important, that many
我是一名优秀的程序员,十分优秀!