首頁 > 後端開發 > php教程 > php處理http post/get請求

php處理http post/get請求

WBOY
發布: 2016-08-08 09:19:06
原創
2812 人瀏覽過

  • 判斷請求類型

$_SERVER[‘REQUEST_METHOD’]
此變數中儲存的是表單提交的方式

<code><span>$method</span> =<span>$_SERVER</span>[<span>'REQUEST_METHOD'</span>];
<span>$data</span> = <span>''</span>;
<span>if</span> (<span>$method</span> == <span>'GET'</span>)
{
    <span>$data</span> = <span>$_SERVER</span>[<span>'QUERY_STRING'</span>];
}
<span>else</span><span>if</span> (<span>$method</span> == <span>'POST'</span>)
{
    <span>$data</span> = file_get_contents(<span>"php://input"</span>);
}
<span>else</span>
{
    <span>$logger</span>-><span>error</span>(<span>'unknown http method. url: '</span> . <span>$_SERVER</span>[<span>'REQUEST_URI'</span>]);
}</code>
登入後複製
  • 取得URL參數
<code><span>//获取域名或主机地址 </span><span>echo</span><span>$_SERVER</span>[<span>'HTTP_HOST'</span>].<span>"<br>"</span>; <span>#localhost</span><span>//获取网页地址 </span><span>echo</span><span>$_SERVER</span>[<span>'PHP_SELF'</span>].<span>"<br>"</span>; <span>#/blog/testurl.php</span><span>//获取网址参数 </span><span>echo</span><span>$_SERVER</span>[<span>"QUERY_STRING"</span>].<span>"<br>"</span>; <span>#id=5</span><span>//获取用户代理 </span><span>echo</span><span>$_SERVER</span>[<span>'HTTP_REFERER'</span>].<span>"<br>"</span>; 

<span>//获取完整的url</span><span>echo</span><span>'http://'</span>.<span>$_SERVER</span>[<span>'HTTP_HOST'</span>].<span>$_SERVER</span>[<span>'REQUEST_URI'</span>];
<span>echo</span><span>'http://'</span>.<span>$_SERVER</span>[<span>'HTTP_HOST'</span>].<span>$_SERVER</span>[<span>'PHP_SELF'</span>].<span>'?'</span>.<span>$_SERVER</span>[<span>'QUERY_STRING'</span>];


<span>//包含端口号的完整url</span><span>echo</span><span>'http://'</span>.<span>$_SERVER</span>[<span>'SERVER_NAME'</span>].<span>':'</span>.<span>$_SERVER</span>[<span>"SERVER_PORT"</span>].<span>$_SERVER</span>[<span>"REQUEST_URI"</span>]; 


<span>//只取路径</span><span>$url</span>=<span>'http://'</span>.<span>$_SERVER</span>[<span>'SERVER_NAME'</span>].<span>$_SERVER</span>[<span>"REQUEST_URI"</span>]; 
<span>echo</span> dirname(<span>$url</span>);</code>
登入後複製

版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。

以上就介紹了php處理http post/get請求,包含了面向的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板