Rumah > pembangunan bahagian belakang > tutorial php > php处理http post/get请求

php处理http post/get请求

WBOY
Lepaskan: 2016-08-08 09:19:06
asal
2812 orang telah melayarinya

  • 判断请求类型

$_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>
Salin selepas log masuk
  • 获取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>
Salin selepas log masuk

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了php处理http post/get请求,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan