{"id":3261,"date":"2024-11-10T08:12:37","date_gmt":"2024-11-10T00:12:37","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3261\/"},"modified":"2024-11-10T08:12:37","modified_gmt":"2024-11-10T00:12:37","slug":"php%e4%b8%ad%e5%a6%82%e4%bd%95%e5%86%99js%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3261\/","title":{"rendered":"php\u4e2d\u5982\u4f55\u5199js\u4ee3\u7801"},"content":{"rendered":"<blockquote><p>\n  \u5728 php \u4e2d\u5d4c\u5165 js \u4ee3\u7801\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528 heredoc\u4f7f\u7528 nowdoc\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5\u4f7f\u7528 php \u51fd\u6570\u4f7f\u7528 php \u5e93\u5d4c\u5165 js \u4ee3\u7801\u53ef\u4ee5\u5728 php \u6587\u4ef6\u4e2d\u4f7f\u7528 echo \u6216 print \u51fd\u6570\uff0c\u6216\u5c06\u5176\u4f20\u9012\u7ed9\u6a21\u677f\u5f15\u64ce\u3002\u540c\u65f6\u8981\u6ce8\u610f\u8f6c\u4e49 js \u4ee3\u7801\u4ee5\u9632\u6b62 xss \u653b\u51fb\uff0c\u5e76\u4ec5\u5d4c\u5165\u5fc5\u8981\u7684\u4ee3\u7801\u4ee5\u4f18\u5316\u52a0\u8f7d\u901f\u5ea6\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110808332858892.jpg\" class=\"aligncenter\" title=\"php\u4e2d\u5982\u4f55\u5199js\u4ee3\u7801\u63d2\u56fe\" alt=\"php\u4e2d\u5982\u4f55\u5199js\u4ee3\u7801\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5411 PHP \u4e2d\u5d4c\u5165 JS \u4ee3\u7801<\/strong><\/p>\n<p><strong>\u65b9\u6cd5 1\uff1a\u4f7f\u7528 heredoc<\/strong><\/p>\n<pre>$js = &lt;&lt;&lt;JS\nconsole.log(\"Hello World!\");\nJS;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 2\uff1a\u4f7f\u7528\u73b0\u5728\u6587\u6863<\/strong><\/p>\n<pre>$js = nowdoc\n    console.log(\"Hello World!\");\n    end nowdoc;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 3\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>$js = \"console.log(\"Hello World!\");\";<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 4\uff1a\u4f7f\u7528 PHP \u51fd\u6570<\/strong><\/p>\n<pre>$js = htmlspecialchars(\"&lt;script&gt;console.log(\"Hello World!\");&lt;\/script&gt;\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 5\uff1a\u4f7f\u7528 PHP \u5e93<\/strong><\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528\u50cf jsminphp \u8fd9\u6837\u7684\u5e93\u6765\u538b\u7f29\u548c\u4f18\u5316 JS \u4ee3\u7801\u3002<\/p>\n<pre>require_once('jsminphp\/jsmin.php');\n$js = Jsmin::minify(\"console.log(\"Hello World!\");\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5728\u54ea\u91cc\u5d4c\u5165 JS \u4ee3\u7801<\/strong><\/p>\n<ul>\n<li>\u5728 PHP \u6587\u4ef6\u4e2d\u4f7f\u7528 echo \u6216 print \u51fd\u6570\u3002<\/li>\n<li>\u5c06 JS \u4ee3\u7801\u5b58\u5165\u4e00\u4e2a PHP \u53d8\u91cf\u4e2d\uff0c\u7136\u540e\u5c06\u5176\u4f20\u9012\u7ed9\u6a21\u677f\u5f15\u64ce\u3002<\/li>\n<li>\u4f7f\u7528 PHP \u6846\u67b6\u6216 CMS\uff08\u5982 Laravel \u6216 WordPress\uff09\u63d0\u4f9b\u7684\u529f\u80fd\u3002<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd JS \u4ee3\u7801\u662f\u7ecf\u8fc7\u8f6c\u4e49\u7684\uff0c\u4ee5\u9632\u6b62 XSS \u653b\u51fb\u3002<\/li>\n<li>\u4ec5\u5d4c\u5165\u5fc5\u8981\u7684 JS \u4ee3\u7801\uff0c\u4ee5\u4f18\u5316\u9875\u9762\u52a0\u8f7d\u901f\u5ea6\u3002<\/li>\n<li>\u4f7f\u7528\u4e00\u4e2a\u597d\u7684\u6587\u672c\u7f16\u8f91\u5668\u6216 IDE \u6765\u7f16\u8f91 JS \u4ee3\u7801\u5e76\u786e\u4fdd\u5176\u6b63\u786e\u7f29\u8fdb\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fphp\u4e2d\u5982\u4f55\u5199js\u4ee3\u7801\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>\u5728 php \u4e2d\u5d4c\u5165 js \u4ee3\u7801\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528 heredoc\u4f7f\u7528 nowdoc\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5\u4f7f\u7528 php \u51fd\u6570\u4f7f\u7528 php \u5e93\u5d4c\u5165 js \u4ee3\u7801\u53ef\u4ee5\u5728 php \u6587\u4ef6\u4e2d\u4f7f\u7528 echo \u6216 print \u51fd\u6570\uff0c\u6216\u5c06\u5176\u4f20\u9012\u7ed9\u6a21\u677f\u5f15\u64ce\u3002\u540c\u65f6\u8981\u6ce8\u610f\u8f6c\u4e49 js \u4ee3\u7801\u4ee5\u9632\u6b62 xss \u653b\u51fb\uff0c\u5e76\u4ec5\u5d4c\u5165\u5fc5\u8981\u7684\u4ee3\u7801\u4ee5\u4f18\u5316\u52a0\u8f7d\u901f\u5ea6\u3002 \u5982\u4f55\u5411 PHP \u4e2d\u5d4c\u5165 JS \u4ee3\u7801 \u65b9\u6cd5 1\uff1a\u4f7f\u7528 heredoc $js = &lt;&lt;&lt;JS console.log(&#8220;Hello World!&#8221;); JS; \u767b\u5f55\u540e\u590d\u5236 \u65b9\u6cd5 2\uff1a\u4f7f\u7528\u73b0\u5728\u6587\u6863 $js = nowdoc console.log(&#8220;Hello World!&#8221;); end nowdoc; \u767b\u5f55\u540e\u590d\u5236 \u65b9\u6cd5 3\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b $js = &#8220;console.log(&#8220;Hello World!&#8221;);&#8221;; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3261","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3261","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=3261"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3261\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}