Home > Backend Development > PHP Tutorial > PHP handles http post/get requests

PHP handles http post/get requests

WBOY
Release: 2016-08-08 09:19:06
Original
2812 people have browsed it

  • Determine the request type

$_SERVER[‘REQUEST_METHOD’]
Stored in this variable is the method of form submission

<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>
Copy after login
  • Getting URL parameters
<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>
Copy after login

Copyright statement: This article is an original article by the blogger and may not be reproduced without the permission of the blogger.

The above introduces how PHP handles http post/get requests, including aspects of the process. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template