{"id":31958,"date":"2024-11-25T10:21:36","date_gmt":"2024-11-25T02:21:36","guid":{"rendered":"https:\/\/fwq.ai\/blog\/31958\/"},"modified":"2024-11-25T10:21:36","modified_gmt":"2024-11-25T02:21:36","slug":"%e5%85%b3%e4%ba%8ewordpress%e4%b8%ad%e7%9a%84nonce%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/31958\/","title":{"rendered":"\u5173\u4e8eWordPress\u4e2d\u7684Nonce\u8be6\u89e3"},"content":{"rendered":"<p>\u4e0b\u9762\u7531\/&#8221; target=&#8221;_blank&#8221;&gt;<strong>wordpress<\/strong>\u6559\u7a0b\u680f\u76ee\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u5173\u4e8ewordpress\u4e2d\u7684nonce\uff0c\u5e0c\u671b\u5bf9\u9700\u8981\u7684\u670b\u53cb\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><strong>WordPress\u4e2d\u7684Nonce<\/strong><\/p>\n<p>Nonce\u662fnumber used once\u7684\u7f29\u5199\uff0cWordpress\u7684nonce\u4e0d\u662f\u6570\u5b57\uff0c\u800c\u4e00\u662f\u4e32\u7531\u6570\u5b57\u548c\u5b57\u7b26\u7ec4\u6210\u7684Hash\u503c\uff0c\u4e0d\u4ec5\u53ea\u80fd\u4f7f\u7528\u4e00\u6b21\uff0c\u8fd8\u540c\u65f6\u5177\u6709\u751f\u547d\u5468\u671f(lifetime)\uff0c\u5728\u751f\u547d\u5468\u671f\u5185\uff0c\u9488\u5bf9\u6bcf\u4e2a\u7528\u6237\uff0c\u540c\u6837\u7684\u53c2\u6570\u4f1a\u751f\u6210\u540c\u6837\u7684nonce\u503c\uff0c\u76f4\u5230\u751f\u547d\u5468\u671f\u7ed3\u675f\u3002\u8fd9\u7bc7\u6587\u7ae0\u6211\u4eec\u5c31\u6765\u4ecb\u7ecd\u4e00\u4e0b\u5982\u4f55\u7528Nonce\u6765\u9632\u6b62CSRF\u653b\u51fb\u3002<\/p>\n<p><strong>\u521b\u5efa\u4e00\u4e2aNonce<\/strong><\/p>\n<p>Nonce\u53ef\u4ee5\u88ab\u653e\u5728Url\u8bf7\u6c42\u4e2d\uff0c\u4e5f\u53ef\u4ee5\u653e\u5728\u4e00\u4e2aForm\u7684Hidden\u5143\u7d20\u4e2d\uff0c\u7136\u540e\u5728Ajax\u8bf7\u6c42\u65f6\uff0c\u901a\u8fc7Javascript\u6765\u83b7\u53d6\u4ed6\u5b83\u3002\u4e00\u4e2aNonce\u751f\u547d\u5468\u671f\u53ea\u5728\u5f53\u524dSession\u4e2d\uff0c\u5982\u679c\u4f60\u9000\u51fa\u767b\u5f55\u540e\u518d\u6b21\u767b\u5f55\uff0c\u4e4b\u524d\u7684nonce\u4e5f\u90fd\u4f1a\u5931\u6548\u3002<\/p>\n<p><strong>\u5411URL\u4e2d\u6dfb\u52a0nonce<\/strong><\/p>\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7wp_nonce_url()\u65b9\u6cd5\u6765\u5411Url\u4e2d\u6dfb\u52a0\u4e00\u4e2aNonce\uff1a<\/p>\n<pre>wp_nonce_url( $actionurl, $action, $name );\n\/\/ \u4f8b\u5982\uff1a\n$complete_url = wp_nonce_url( $bare_url, 'trash-post_'.$post-&gt;ID );<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d$bare_url\uff08\u5fc5\u9009\uff09\u4e3a\u8981\u6dfb\u52a0nonce\u7684url\uff0c\u800c$action\u4e3a\u4e3anonce\u5b9a\u4e49\u7684\u52a8\u4f5c\u540d\u5b57\uff0c\u53ef\u9009\uff0c\u9ed8\u8ba4\u4e3a-1\u3002<\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u751f\u6210\u7684nonce\u5728\u94fe\u63a5\u4e2d\u7684\u540d\u5b57\u4e3a_wpnonce\uff0c\u4e3a\u4e86\u907f\u514d\u53ef\u80fd\u7684\u51b2\u7a81\uff0c\u5728Wordpress3.6\u7248\u672c\u540e\uff0cwp_nonce_url\u589e\u52a0\u4e86\u53ef\u9009\u7684$name\u53c2\u6570\uff0c\u53ef\u4ee5\u8ba9\u7528\u6237\u81ea\u5df1\u6307\u5b9anonce\u5728\u94fe\u63a5\u4e2d\u7684\u540d\u5b57\u3002\u5982\uff1a<\/p>\n<pre>$complete_url = wp_nonce_url( $bare_url, 'trash-post_'.$post-&gt;ID, 'my_nonce' );<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5411Form\u4e2d\u6dfb\u52a0nonce<\/strong><\/p>\n<p>\u4f60\u53ef\u4ee5\u80fd\u8fc7wp_nonce_field()\u65b9\u6cd5\u5411\u8868\u5355\u4e2d\u6dfb\u52a0\u4e00\u4e2ahidden\u5143\u7d20\uff1a<\/p>\n<pre>PHP\n\nwp_nonce_field( $action, $name, $referer, $echo )\n\/\/\u4f8b\u5982 \uff1a\nwp_nonce_field( 'delete-comment_'.$comment_id );\nwp_nonce_field( $action, $name, $referer, $echo )\n\/\/\u4f8b\u5982 \uff1a\nwp_nonce_field( 'delete-comment_'.$comment_id );<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8c03\u7528\u4e0a\u9762\u7684\u65b9\u6cd5\uff0c\u4f1a\u751f\u6210\u7c7b\u4f3c\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>&lt;input type=\"hidden\" id=\"_wpnonce\" name=\"_wpnonce\" value=\"796c7766b1\" \/&gt;\n&lt;input type=\"hidden\" name=\"_wp_http_referer\" value=\"\/wp-admin\/edit-comments.php\" \/&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5355\u72ec\u751f\u6210\u4e00\u4e2anonce<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u53ea\u662f\u60f3\u8981\u751f\u6210\u4e00\u4e2a\u72ec\u7acb\u7684nonce\uff0c\u53ef\u4ee5\u8fc7wp_create_nonce()\u65b9\u6cd5\uff1a<\/p>\n<pre>wp_create_nonce( $action );\n\/\/ \u4f8b\u5982\uff1a\n$nonce = wp_create_nonce( 'my-action_'.$post-&gt;ID );<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u540c\u6837\u7684\uff0c$action\u4e3a\u53ef\u9009\u53c2\u6570\uff0c\u9ed8\u8ba4\u4e3a-1\u3002\u4e0a\u9762\u7684\u65b9\u6cd5\u4f1a\u8fd4\u56de\u7c7b\u4f3c\u201c295a686963\u201d\u7684\u7ed3\u679c\u3002<\/p>\n<p><strong>\u9a8c\u8bc1nonce\u6709\u6548\u6027<\/strong><\/p>\n<p><strong>\u9a8c\u8bc1\u8868\u5355\u4e2d\u7684nonce<\/strong><\/p>\n<p>\u5728Admin\u7ba1\u7406\u754c\u9762\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7check_admin_referer\u65b9\u6cd5\u6765\u9a8c\u8bc1Url\u4e2dNonce\u7684\u6709\u6548\u6027\uff1a<\/p>\n<pre>check_admin_referer( $action, $query_arg );<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\u6f14\u793a\u5982\u4f55\u5728\u63d2\u4ef6\u4e2d\u4f7f\u7528check_admin_referer\u9a8c\u8bc1nonce\uff1a<\/p>\n<pre>&lt;form method=\"post\"&gt;\n   &lt;!-- some inputs here --&gt;\n   &lt;?php wp_nonce_field( 'name_of_my_action', 'name_of_nonce_field' ); ?&gt;\n&lt;\/form&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u9a8c\u8bc1\u65b9\u6cd5\uff1a<\/p>\n<pre>check_admin_referer( 'name_of_my_action', 'name_of_nonce_field' );<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9a8c\u8bc1Ajax\u4e2d\u7684nonce<\/strong><\/p>\n<p>\u5982\u679c\u8981\u68c0\u67e5Ajax\u8bf7\u6c42\u4e2d\u7684nonce\u6709\u6548\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528check_ajax_referer()\u65b9\u6cd5\uff1a<\/p>\n<pre>check_ajax_referer( $action, $query_arg, $die )<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>$die\u6307\u5b9a\u5982\u679c$nonce\u65e0\u6548\uff0c\u662f\u5426\u7ed3\u675f\u811a\u672c\u6267\u884c\u3002\uff08\u9ed8\u8ba4\u4e3aTrue\uff09<\/p>\n<p>\u4e00\u4e2a\u7b80\u5355\u4f7f\u7528check_ajax_referer\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre>&lt;?php\n\/\/Set Your Nonce\n$ajax_nonce = wp_create_nonce( \"my-special-string\" );\n?&gt;\n \n&lt;script type=\"text\/javascript\"&gt;\njQuery(document).ready(function($){\n    var data = {\n        action: 'my_action',\n        security: '&lt;?php echo $ajax_nonce; ?&gt;',\n        my_string: 'Hello World!'\n    };\n    $.post(ajaxurl, data, function(response) {\n        alert(\"Response: \" + response);\n    });\n});\n&lt;\/script&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u5411\u540e\u901a\u8fc7\u4e0b\u9762\u7684\u4ee3\u7801\u8fdb\u884c\u9a8c\u8bc1\uff1a<\/p>\n<pre>add_action( 'wp_ajax_my_action', 'my_action_function' );\nfunction my_action_function() {\n    check_ajax_referer( 'my-special-string', 'security' );\n    echo sanitize_text_field( $_POST['my_string'] );\n    wp_die();\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9a8c\u8bc1\u72ec\u7acb\u751f\u6210\u7684nonce<\/strong><\/p>\n<pre>1\nwp_verify_nonce( $nonce, $action );<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5173\u4e8eWordPress\u4e2d\u7684Nonce\u8be6\u89e3\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7531\/&#8221; target=&#8221;_blank&#8221;&gt;wordpress\u6559\u7a0b\u680f\u76ee\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u5173\u4e8ewordpress\u4e2d\u7684nonce\uff0c\u5e0c\u671b\u5bf9\u9700\u8981\u7684\u670b\u53cb\u6709\u6240\u5e2e\u52a9\uff01 WordPress\u4e2d\u7684Nonce Nonce\u662fnumber used once\u7684\u7f29\u5199\uff0cWordpress\u7684nonce\u4e0d\u662f\u6570\u5b57\uff0c\u800c\u4e00\u662f\u4e32\u7531\u6570\u5b57\u548c\u5b57\u7b26\u7ec4\u6210\u7684Hash\u503c\uff0c\u4e0d\u4ec5\u53ea\u80fd\u4f7f\u7528\u4e00\u6b21\uff0c\u8fd8\u540c\u65f6\u5177\u6709\u751f\u547d\u5468\u671f(lifetime)\uff0c\u5728\u751f\u547d\u5468\u671f\u5185\uff0c\u9488\u5bf9\u6bcf\u4e2a\u7528\u6237\uff0c\u540c\u6837\u7684\u53c2\u6570\u4f1a\u751f\u6210\u540c\u6837\u7684nonce\u503c\uff0c\u76f4\u5230\u751f\u547d\u5468\u671f\u7ed3\u675f\u3002\u8fd9\u7bc7\u6587\u7ae0\u6211\u4eec\u5c31\u6765\u4ecb\u7ecd\u4e00\u4e0b\u5982\u4f55\u7528Nonce\u6765\u9632\u6b62CSRF\u653b\u51fb\u3002 \u521b\u5efa\u4e00\u4e2aNonce Nonce\u53ef\u4ee5\u88ab\u653e\u5728Url\u8bf7\u6c42\u4e2d\uff0c\u4e5f\u53ef\u4ee5\u653e\u5728\u4e00\u4e2aForm\u7684Hidden\u5143\u7d20\u4e2d\uff0c\u7136\u540e\u5728Ajax\u8bf7\u6c42\u65f6\uff0c\u901a\u8fc7Javascript\u6765\u83b7\u53d6\u4ed6\u5b83\u3002\u4e00\u4e2aNonce\u751f\u547d\u5468\u671f\u53ea\u5728\u5f53\u524dSession\u4e2d\uff0c\u5982\u679c\u4f60\u9000\u51fa\u767b\u5f55\u540e\u518d\u6b21\u767b\u5f55\uff0c\u4e4b\u524d\u7684nonce\u4e5f\u90fd\u4f1a\u5931\u6548\u3002 \u5411URL\u4e2d\u6dfb\u52a0nonce \u4f60\u53ef\u4ee5\u901a\u8fc7wp_nonce_url()\u65b9\u6cd5\u6765\u5411Url\u4e2d\u6dfb\u52a0\u4e00\u4e2aNonce\uff1a wp_nonce_url( $actionurl, $action, $name ); \/\/ \u4f8b\u5982\uff1a $complete_url = wp_nonce_url( $bare_url, &#8216;trash-post_&#8217;.$post-&gt;ID ); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d$bare_url\uff08\u5fc5\u9009\uff09\u4e3a\u8981\u6dfb\u52a0nonce\u7684url\uff0c\u800c$action\u4e3a\u4e3anonce\u5b9a\u4e49\u7684\u52a8\u4f5c\u540d\u5b57\uff0c\u53ef\u9009\uff0c\u9ed8\u8ba4\u4e3a-1\u3002 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u751f\u6210\u7684nonce\u5728\u94fe\u63a5\u4e2d\u7684\u540d\u5b57\u4e3a_wpnonce\uff0c\u4e3a\u4e86\u907f\u514d\u53ef\u80fd\u7684\u51b2\u7a81\uff0c\u5728Wordpress3.6\u7248\u672c\u540e\uff0cwp_nonce_url\u589e\u52a0\u4e86\u53ef\u9009\u7684$name\u53c2\u6570\uff0c\u53ef\u4ee5\u8ba9\u7528\u6237\u81ea\u5df1\u6307\u5b9anonce\u5728\u94fe\u63a5\u4e2d\u7684\u540d\u5b57\u3002\u5982\uff1a $complete_url = wp_nonce_url( $bare_url, &#8216;trash-post_&#8217;.$post-&gt;ID, &#8216;my_nonce&#8217; ); \u767b\u5f55\u540e\u590d\u5236 \u5411Form\u4e2d\u6dfb\u52a0nonce \u4f60\u53ef\u4ee5\u80fd\u8fc7wp_nonce_field()\u65b9\u6cd5\u5411\u8868\u5355\u4e2d\u6dfb\u52a0\u4e00\u4e2ahidden\u5143\u7d20\uff1a PHP wp_nonce_field( $action, $name, $referer, $echo ) \/\/\u4f8b\u5982 \uff1a wp_nonce_field( &#8216;delete-comment_&#8217;.$comment_id ); wp_nonce_field( $action, $name, $referer, $echo ) \/\/\u4f8b\u5982 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-31958","post","type-post","status-publish","format-standard","hentry","category-cms"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31958","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=31958"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31958\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=31958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=31958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=31958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}