{"id":72,"date":"2016-06-03T08:51:04","date_gmt":"2016-06-03T00:51:04","guid":{"rendered":"http:\/\/www.tra56.com\/?p=72"},"modified":"2016-06-03T08:51:04","modified_gmt":"2016-06-03T00:51:04","slug":"php-determine-if-a-request-is-a-ajax-request-or-a-request","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/72.html","title":{"rendered":"PHP\u5224\u65ad\u4e00\u4e2a\u8bf7\u6c42\u662fAJAX\u8bf7\u6c42\u8fd8\u662f\u666e\u901a\u8bf7\u6c42"},"content":{"rendered":"

\n\t\u5728PHP\u7a0b\u5e8f\u4e2d\uff0c\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u7f51\u9875\u8bf7\u6c42\u662fajax\u8bf7\u6c42\u8fd8\u662f\u666e\u901a\u8bf7\u6c42\uff1f\u81ea\u4ece\u6211\u505a\u7684\u4e00\u4e2a\u8f66\u7f51\u9879\u76ee\u4ee5\u6765\uff0c\u8fd9\u4e2a\u95ee\u9898\u56f0\u6270\u4e86\u6211\u5f88\u4e45\u4e86\uff0c\u539f\u56e0\u662f\u8fd9\u6837\u7684\uff0c\u6211\u7684\u90a3\u4e2a\u8f66\u7f51\u9879\u76ee\uff0c\u6709\u4e00\u4e2a\u56db\u7ea7\u8054\u52a8\u7684\u8f66\u578b\u68c0\u7d22\uff0c\u5fc5\u987b\u5f97\u7528ajax\u5b9e\u73b0\u3002\u529f\u80fd\u662f\u5b9e\u73b0\u7684\u86ee\u6f02\u4eae\u7684\uff0c\u4f46\u662f\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u6211\u53d1\u73b0\u5728\u6d4f\u89c8\u5668\u4e2d\u76f4\u63a5\u8f93\u5165\u6211ajax\u63d0\u4ea4\u7684\u5730\u5740\u5e26\u4e0a\u4e00\u4e9b\u8f66\u578b\u53c2\u6570\uff0c\u6d4f\u89c8\u5668\u5c31\u80fd\u628a\u6570\u636e\u6253\u5370\u51fa\u6765\u3002\u6211\u89c9\u5f97\u8fd9\u6837\u975e\u5e38\u4e0d\u597d\u3002
\n<\/span>\n\t<\/p>\n

\n\t\t\u4f46\u662f\u7531\u4e8e\u6c34\u5e73\u6709\u9650\uff0c\u8fd9\u4e2a\u95ee\u9898\u76f4\u5230\u4eca\u5929\u624d\u88ab\u6211\u89e3\u51b3\uff0c\u7279\u6b64\u7559\u4e0b\u6587\u7ae0\u4ee5\u4f9b\u6709\u6b64\u9700\u8981\u7684\u4eba\u5b66\u4e60\u3002\n\t<\/p>\n

\n\t\t\n\t<\/p>\n

\n\tDEMO\u770b\u8fc7\u4e86\u5427\uff1f\u4e0d\u77e5\u9053\u60a8\u6709\u6ca1\u6709\u53d1\u73b0\u666e\u901a\u8bf7\u6c42\u548cajax\u8bf7\u6c42\u7684\u7ed3\u679c\u6709\u4ec0\u4e48\u4e0d\u4e00\u6837\uff1f\u5475\u5475~\u6ca1\u4e8b\uff0c\u6ca1\u770b\u660e\u767d\u7684\uff0c\u4e0b\u9762\u6211\u4f1a\u8be6\u7ec6\u8bf4\u8bf4\u662f\u600e\u4e48\u5b9e\u73b0\u7684\u3002\n<\/p>\n

\n\t\u9996\u5148\u8bf4\u8bf4\u539f\u7406\uff1a
\n\u5728\u53d1\u9001ajax\u8bf7\u6c42\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7XMLHttpRequest\u8fd9\u4e2a\u5bf9\u8c61\uff0c\u521b\u5efa\u81ea\u5b9a\u4e49\u7684header\u5934\u4fe1\u606f<\/strong>
\n\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662f\u539f\u751f\u7684ajax\u65b9\u6cd5\uff0c\u4e5f\u5c31\u662f\u672a\u4f7f\u7528jquery\u6216\u8005\u5176\u4ed6js\u6846\u67b6\u5305\u88c5\u7684ajax\u65b9\u6cd5\uff0c\u90a3\u4e48\u4ee3\u7801\u5982\u4e0b\uff1a\n<\/p>\n

\n
\n
\n\t\t\t?<\/a><\/span>\n\t\t<\/div>\n
\n\n
\n
\n
\n\t\t\t\t\t\t\t1\n\t\t\t\t\t\t<\/div>\n<\/td>\n
\n
\n
\n\t\t\t\t\t\t\t\txmlHttpRequest.setRequestHeader(\"request_type\",\"ajax\"); \n\t\t\t\t\t\t\t<\/div>\n<\/p><\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table><\/div>\n<\/div>\n

\n\t\u54e6~\u5bf9\u4e86\uff0c\u60a8\u4e00\u5b9a\u77e5\u9053xmlHttpRequest\u8fd9\u4e2a\u5bf9\u8c61\u662f\u600e\u4e48\u521b\u5efa\u7684\u5427\uff1f\u989d\uff1f\u60a8\u6ca1\u5f00\u73a9\u7b11\u5427\uff1f\u60a8\u4e0d\u77e5\u9053\u2013\u90a3\u6211\u529d\u60a8\u8fd8\u662f\u4e0d\u8981\u4e86\u89e3\u4e86\uff0c\u653e\u5fc3\u5427~\u60a8\u5bf9\u4ed6\u4e0d\u4e86\u89e3\u4e5f\u6ca1\u4e8b\uff0c\u4e0b\u9762\u6211\u6765\u544a\u8bc9\u4f60\u600e\u4e48\u5728jquery\u4e2d\u4f7f\u7528\uff0c\u90a3\u662f\u66f4\u52a0\u7684\u7b80\u5355\u554a~~\n<\/p>\n

\n\t\u901a\u8fc7jquery\u7684$.ajax()\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u53d1\u9001ajax\u8bf7\u6c42\u4e4b\u524d\uff0c\u521b\u5efa\u6211\u4eec\u81ea\u5b9a\u4e49\u7684header\u5934\u4fe1\u606f\u3002\n<\/p>\n

\n
\n
\n\t\t\t?<\/a><\/span>\n\t\t<\/div>\n
\n\n
\n
\n
\n\t\t\t\t\t\t\t1\n\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t2\n\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t3\n\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t4\n\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t5\n\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t6\n\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t7\n\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t8\n\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t9\n\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t10\n\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t11\n\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t12\n\t\t\t\t\t\t<\/div>\n<\/td>\n
\n
\n
\n\t\t\t\t\t\t\t\t$.ajax({\n\t\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t\t    type        :\"GET\",\n\t\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t\t    url     : base_url +'php_check_ajax_request\/get_user_list.html',\n\t\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t\t    beforeSend  :function(XMLHttpRequest) {\n\t\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t\t        XMLHttpRequest.setRequestHeader(\"request_type\",\"ajax\");\n\t\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t\t    },\n\t\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t\t    success     :function(data){\n\t\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t\t        $(\"#user_list\").html(data);\n\t\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t\t        $tip.hide();\n\t\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t\t        $button.attr('disabled',true);\n\t\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t\t    }\n\t\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t<\/div>\n<\/p><\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table><\/div>\n<\/div>\n

\n\t\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u662f\u6211\u7684demo\u4e2d\u7528\u5230\u7684\u4ee3\u7801\uff0c\u6709\u597d\u51e0\u884c\u5427\uff0c\u60a8\u522b\u6015\uff0c\u8981\u60a8\u6ce8\u610f\u7684\u5c31\u662f\u4e00\u4e2a\u5730\u5740\uff0c\u627e\u5230\u4e86beforeSend\u4e86\u5427\uff1f\u5bf9\u4e86\uff0c\u5c31\u662f\u90a3\u91cc\u4e86\uff0cXMLHttpRequest.setRequestHeader(\u201crequest_type\u201d,\u201dajax\u201d);\u8fd9\u4e00\u53e5\u4ee3\u7801\u5c31\u662f\u5411header\u5934\u4fe1\u606f\u91cc\u9762\u521b\u5efa\u4e86\u4e00\u4e2a\u6211\u4eec\u81ea\u5b9a\u4e49\u7684\u4e00\u4e2a\u53d8\u91cf\u201crequest_type\u201d\u3002\n<\/p>\n

\n\t\u6069~~\u8bf4\u5230\u8fd9\u91cc\uff0c\u5927\u5bb6\u5e94\u8be5\u5df2\u7ecf\u4f1a\u5728\u53d1\u9001ajax\u8bf7\u6c42\u524d\u5f80header\u5934\u4fe1\u606f\u4e2d\u52a0\u5165\u81ea\u5b9a\u7684\u4fe1\u606f\u4e86\u5427\u3002
\n\u63a5\u4e0b\u6765\u6211\u6765\u544a\u8bc9\u5927\u5bb6\u5982\u679c\u5728php\u4e2d\u53d6\u5f97\u8fd9\u4e2a\u4fe1\u606f\uff0c\u4e5f\u662f\u76f8\u5f53\u7b80\u5355\u5440
\n\u5982\u6211\u4e4b\u524d\u5b9a\u4e49\u7684\u90a3\u4e2a\u53d8\u91cf\u201crequest_type\u201d,\u90a3\u4e48\u5728php\u4e2d\u5c31\u5e94\u8be5\u8fd9\u6837\u53d6\u5f97\n<\/p>\n

\n
\n
\n\t\t\t?<\/a><\/span>\n\t\t<\/div>\n
\n\n
\n
\n
\n\t\t\t\t\t\t\t1\n\t\t\t\t\t\t<\/div>\n<\/td>\n
\n
\n
\n\t\t\t\t\t\t\t\t$_SERVER['HTTP_REQUEST_TYPE']\n\t\t\t\t\t\t\t<\/div>\n<\/p><\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table><\/div>\n<\/div>\n

\n\t\u770b\u4e86\u4e0a\u9762\u7684\u4ee3\u7801\u4e4b\u540e\uff0c\u6211\u60f3\u5982\u6b64\u777f\u667a\u7684\u60a8\u4e00\u5b9a\u5df2\u7ecf\u53d1\u73b0\u89c4\u5f8b\u4e86\u5427\uff0c\u5bf9\u4e86\uff0c\u6ca1\u9519\uff0c\u5c31\u662f\u5c06\u201crequest_type\u201d\u8fd9\u4e2a\u53d8\u91cf\u5168\u90e8\u5927\u5199\uff08\u4e00\u5b9a\u8981\u5927\u5199\uff09\uff0c\u7136\u540e\u518d\u52a0\u4e0a\u4e00\u4e2a\u201cHTTP_\u201d\u524d\u7f00\uff0c\u7136\u540e\u518d$_SERVER\u8fd9\u4e2a\u8d85\u7ea7\u53d8\u91cf\u4e2d\u627e\u5c31\u53ef\u4ee5\u4e86\u3002\n<\/p>\n

\n
\n
\n\t\t\t?<\/a><\/span>\n\t\t<\/div>\n
\n\n
\n
\n
\n\t\t\t\t\t\t\t1\n\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t2\n\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t3\n\t\t\t\t\t\t<\/div>\n<\/td>\n
\n
\n
\n\t\t\t\t\t\t\t\tif(isset($_SERVER['HTTP_REQUEST_TYPE']) &&$_SERVER['HTTP_REQUEST_TYPE'] ==\"ajax\"){\/\/ajax\u63d0\u4ea4\n\t\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t\t}else{\/\/\u975eajax\u63d0\u4ea4\n\t\t\t\t\t\t\t<\/div>\n
\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t<\/div>\n<\/p><\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table><\/div>\n<\/div>\n

\n\t\u4e0a\u9762\u7684\u4ee3\u7801\u5c31\u662f\u5728php\u7a0b\u5e8f\u4e2d\u5224\u65ad\u8bf7\u6c42\u5230\u5e95\u662f\u4e0d\u662fajax\u8bf7\u6c42\u4e86\u3002\n<\/p>\n

\n\t\u201crequest_type\u201d\u8fd9\u4e2a\u53d8\u91cf\u662f\u6211\u4eec\u81ea\u5b9a\u4e49\u7684\u54e6\uff0c\u5982\u679c\u60a8\u559c\u6b22\u7684\u8bdd\uff0c\u60a8\u4e5f\u53ef\u4ee5\u5c06\u5979\u6362\u6210\u4f60\u6240\u559c\u6b22\u7684\u540d\u5b57\uff0c\u6bd4\u5982\u201ctest\u201d,\u201cis_ajax\u201d\u2026\u2026.\n<\/p>\n

\n\t\u57fa\u4e8e\u4ee5\u4e0a\u7684\u8bf4\u660e~~\u5927\u5bb6\u662f\u5426\u660e\u767d\u4e86\u5462\uff1f<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728PHP\u7a0b\u5e8f\u4e2d\uff0c\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u7f51\u9875\u8bf7\u6c42\u662fajax\u8bf7\u6c42\u8fd8\u662f\u666e\u901a\u8bf7\u6c42\uff1f\u81ea\u4ece\u6211\u505a\u7684\u4e00\u4e2a\u8f66\u7f51\u9879\u76ee\u4ee5\u6765\uff0c\u8fd9\u4e2a\u95ee\u9898\u56f0\u6270\u4e86\u6211\u5f88 […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,10],"tags":[],"class_list":["post-72","post","type-post","status-publish","format-standard","hentry","category-html","category-php"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/72"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=72"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/72\/revisions"}],"predecessor-version":[{"id":73,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/72\/revisions\/73"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=72"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=72"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}