{"id":452,"date":"2024-12-19T17:41:04","date_gmt":"2024-12-19T09:41:04","guid":{"rendered":"https:\/\/znl.pub\/?p=452"},"modified":"2025-01-14T15:15:22","modified_gmt":"2025-01-14T07:15:22","slug":"%e3%80%90%e5%b9%b2%e8%b4%a7%e5%90%88%e9%9b%86%e3%80%91php%e6%8a%80%e5%b7%a7%e6%94%b6%e9%9b%86","status":"publish","type":"post","link":"https:\/\/znl.pub\/?p=452","title":{"rendered":"\u3010\u5e72\u8d27\u5408\u96c6\u3011PHP\u6280\u5de7\u6536\u96c6"},"content":{"rendered":"\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>\u7528\u6237\u5bc6\u7801\u52a0\u5bc6\u5b58\u50a8\u4e0e\u6821\u9a8c\uff08bcrypt\uff09<\/summary>\n<p>PHP\u539f\u751f\u63d0\u4f9b\u4e86\u5bc6\u7801\u6821\u9a8c\u5957\u4ef6\uff0cpassword_hash\u3001password_hash \u51fd\u6570\u3002<\/p>\n\n\n\n<p>\u67e5\u770b\u5f53\u524d\u652f\u6301\u90a3\u4e9bpassword hash\u7b97\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nprint_r( password_algos());\/\/\u6253\u5370password_hash()\u652f\u6301\u7684\u7b97\u6cd5\n\/*Array\n(\n    &#91;0] =&gt; 2y \/\/\u5e38\u91cf\uff1a PASSWORD_BCRYPT,PASSWORD_DEFAULT(\u5f53\u524d)\n    &#91;1] =&gt; argon2i \/\/\u5e38\u91cf\uff1a PASSWORD_ARGON2I\n    &#91;2] =&gt; argon2id \/\/\u5e38\u91cf\uff1a PASSWORD_ARGON2ID\n)*\/\n?&gt;<\/code><\/pre>\n\n\n\n<p>\u9ed8\u8ba4\u7b97\u6cd5\uff08\u76ee\u524d\u662f bcrypt\uff09,PASSWORD_DEFAULT(=&#8221;2y&#8221;)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$password=\"123\";\n$hash_bcrypt= password_hash($password, PASSWORD_DEFAULT);\necho $hash_bcrypt;\n\/\/$2y$10$lqIW4KIBR6ZBF2wHYzDY7ez9dL4DemosVLH6ZcY\/RYWzINJaq3rli\n\/\/\u9ed8\u8ba4\u65f6\uff0c\u5de5\u4f5c\u56e0\u5b50cost\u4f1a\u9ed8\u8ba4\u53d610\u3002\nvar_dump(password_verify('123',$hash_bcrypt));\n\/\/bool(true)\n?><\/code><\/pre>\n\n\n\n<p>bcrypt \u7b97\u6cd5,PASSWORD_BCRYPT\uff08=&#8221;2y&#8221;\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$password=\"123\";\n$hash_bcrypt=password_hash($password,PASSWORD_BCRYPT\uff0c&#91;'cost'=&gt;rand(4,31)]);\/\/\u9ed8\u8ba4cost=10\necho $hash_bcrypt;\n\/\/$2y$12$A0fTxKgT14PZM4m391bM8.UoYxdzk0H2AiA8uqyo3FxtIWAk32fzu\n\/*\n$2y$ \u8868\u793a\u54c8\u5e0c\u7b97\u6cd5\uff0c\u4ed6\u662f\u4e00\u79cdbcrypt\u5b9e\u73b0\u7248\u672c\u3002\n12$ \u8868\u793a\u5de5\u4f5c\u56e0\u5b50\uff08cost factor\uff09\uff0c\u8868\u793a 2 \u7684\u5e42\u6b21\uff0c\u503c\u8d8a\u5927\u8ba1\u7b97\u65f6\u95f4\u8d8a\u957f\u3002\u8fd9\u91cc\u8868\u793a 2^12 \u6b21\u8fed\u4ee3\u3002\nA0fTxKgT14PZM4m391bM8.\uff0c\u8fd9\u662f 22 \u4e2a\u5b57\u7b26\u7684 Base64 \u7f16\u7801\u7684\u76d0\u503c\uff08salt\uff09\u3002\nUoYxdzk0H2AiA8uqyo3FxtIWAk32fzu\uff0c\u8fd9\u662f\u5b9e\u9645\u7684\u54c8\u5e0c\u503c\u90e8\u5206\uff0c\u4e5f\u662f 31 \u4e2a\u5b57\u7b26\u7684 Base64 \u7f16\u7801\u3002\n*\/\nvar_dump(password_verify('123',$hash_bcrypt));\n\/\/bool(true)\n\n<\/code><\/pre>\n<\/details>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>\u5927\u6587\u4ef6\u4e0b\u8f7d\uff08\u4e0d\u52a0\u8f7d\u5230php\u5185\u5b58\u7684\uff09<\/summary>\n<p>readfile \u51fd\u6570,\u53ef\u4ee5\u76f4\u63a5\u8bfb\u53d6\u6587\u4ef6\u5e76\u5c06\u5176\u8f93\u51fa\u5230\u6d4f\u89c8\u5668\uff0c\u800c\u4e0d\u4f1a\u5c06\u6574\u4e2a\u6587\u4ef6\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002\u5b83\u975e\u5e38\u9002\u5408\u7528\u4e8e\u4e0b\u8f7d\u5927\u6587\u4ef6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u6587\u4ef6\u8def\u5f84\n$file_path = '\/path\/to\/large-file.zip';\n\n\/\/ \u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\nif (!file_exists($file_path)) {\n    die('\u6587\u4ef6\u4e0d\u5b58\u5728');\n}\n\n\/\/ \u8bbe\u7f6e\u9002\u5f53\u7684 HTTP \u5934\u4fe1\u606f\nheader('Content-Description: File Transfer');\nheader('Content-Type: application\/octet-stream');\nheader('Content-Disposition: attachment; filename=\"' . basename($file_path) . '\"');\nheader('Expires: 0');\nheader('Cache-Control: must-revalidate');\nheader('Pragma: public');\nheader('Content-Length: ' . filesize($file_path));\n\n\/\/ \u7981\u7528\u8f93\u51fa\u7f13\u51b2\uff0c\u786e\u4fdd\u6570\u636e\u7acb\u5373\u53d1\u9001\u5230\u5ba2\u6237\u7aef\nob_end_clean();\n\n\/\/ \u4f7f\u7528 readfile \u76f4\u63a5\u8bfb\u53d6\u5e76\u8f93\u51fa\u6587\u4ef6\nreadfile($file_path);\n\n\/\/ \u786e\u4fdd\u811a\u672c\u5728\u6b64\u5904\u7ec8\u6b62\nexit;\n?&gt;<\/code><\/pre>\n\n\n\n<p>fpassthru \u51fd\u6570\u53ef\u4ee5\u4ece\u6587\u4ef6\u6307\u9488\u4e2d\u8bfb\u53d6\u6570\u636e\u5e76\u76f4\u63a5\u8f93\u51fa\u5230\u6d4f\u89c8\u5668\uff0c\u7c7b\u4f3c\u4e8e readfile\uff0c\u4f46\u5b83\u5141\u8bb8\u4f60\u66f4\u7075\u6d3b\u5730\u63a7\u5236\u6587\u4ef6\u7684\u8bfb\u53d6\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u6587\u4ef6\u8def\u5f84\n$file_path = '\/path\/to\/large-file.zip';\n\n\/\/ \u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\nif (!file_exists($file_path)) {\n    die('\u6587\u4ef6\u4e0d\u5b58\u5728');\n}\n\n\/\/ \u8bbe\u7f6e\u9002\u5f53\u7684 HTTP \u5934\u4fe1\u606f\nheader('Content-Description: File Transfer');\nheader('Content-Type: application\/octet-stream');\nheader('Content-Disposition: attachment; filename=\"' . basename($file_path) . '\"');\nheader('Expires: 0');\nheader('Cache-Control: must-revalidate');\nheader('Pragma: public');\nheader('Content-Length: ' . filesize($file_path));\n\n\/\/ \u6253\u5f00\u6587\u4ef6\n$file = fopen($file_path, 'rb');\n\n\/\/ \u7981\u7528\u8f93\u51fa\u7f13\u51b2\uff0c\u786e\u4fdd\u6570\u636e\u7acb\u5373\u53d1\u9001\u5230\u5ba2\u6237\u7aef\nob_end_clean();\n\n\/\/ \u4f7f\u7528 fpassthru \u4ece\u6587\u4ef6\u6307\u9488\u4e2d\u8bfb\u53d6\u5e76\u8f93\u51fa\u6570\u636e\nfpassthru($file);\n\n\/\/ \u5173\u95ed\u6587\u4ef6\nfclose($file);\n\n\/\/ \u786e\u4fdd\u811a\u672c\u5728\u6b64\u5904\u7ec8\u6b62\nexit;\n?&gt;<\/code><\/pre>\n<\/details>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>\u83b7\u53d6\u6bcf\u6b21\u8bf7\u6c42\u90fd\u4e0d\u4f1a\u91cd\u590d\u7684\u8d85\u77ed\u5b57\u7b26\u4e32<\/summary>\n<p><strong>\u65f6\u95f4\u6233 + Base36 \u7f16\u7801<\/strong><\/p>\n\n\n\n<p>Base36 \u7f16\u7801\u53ef\u4ee5\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u66f4\u77ed\u7684\u5b57\u7b26\u4e32\uff0c\u540c\u65f6\u4fdd\u6301\u552f\u4e00\u6027\u3002\u56e0\u6b64\u53ef\u4ee5\u5c06\u65f6\u95f4\u6233\u548c\u4e00\u4e2a\u968f\u673a\u6570\u7ec4\u5408\u540e\u8fdb\u884c Base36 \u7f16\u7801\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nfunction generateUniqueKeyBase36() {\n    \/\/ \u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233\uff08\u6beb\u79d2\uff09\n    $timestamp = round(microtime(true) * 1000);\n    \/\/ \u751f\u6210\u4e00\u4e2a4\u4f4d\u968f\u673a\u6570\uff080-9999\uff09\n    $randomNumber = random_int(0, 9999);\n    \/\/ \u7ec4\u5408\u6210\u4e00\u4e2a\u8f83\u5927\u7684\u6570\u5b57\n    $combined = $timestamp * 10000 + $randomNumber;\n    \/\/ \u5c06\u5176\u8f6c\u6362\u4e3a Base36 \u7f16\u7801\n    return base_convert($combined, 10, 36);\n}\n\n\/\/ \u793a\u4f8b\u8f93\u51fa\uff1a\u4e00\u4e2a\u8f83\u77ed\u7684\u552f\u4e00\u5b57\u7b26\u4e32\necho generateUniqueKeyBase36();\n\/\/h2v6rayd7o<\/code><\/pre>\n<\/details>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,22],"tags":[],"class_list":["post-452","post","type-post","status-publish","format-standard","hentry","category-php","category-22"],"_links":{"self":[{"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/posts\/452","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/znl.pub\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=452"}],"version-history":[{"count":15,"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/posts\/452\/revisions"}],"predecessor-version":[{"id":558,"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/posts\/452\/revisions\/558"}],"wp:attachment":[{"href":"https:\/\/znl.pub\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/znl.pub\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/znl.pub\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}