Home  >  Article  >  Backend Development  >  Usage of $_GET, $_POST, $_REQUEST and $_SERVER in php

Usage of $_GET, $_POST, $_REQUEST and $_SERVER in php

WBOY
WBOYOriginal
2016-07-25 08:56:331420browse
  1. echo $_GET['xingming']."
    ";
  2. echo $_GET['age']."< ;br>";
  3. echo $_POST['xingming']."
    ";
  4. echo $_POST['age']."
    ";
  5. ?>
Copy code Official description of

$_REQUEST: $_REQUEST — The HTTP Request variable contains an array of $_GET, $_POST, and $_COOKIE by default. Regardless of whether it comes from get or post, if you want to get the value of a certain key, just use $_REQUEST. However, the speed of $_REQUEST will be slightly slower than $_GET and $_POST. Get server-side information through $_SERVER $_SERVER is an array that contains headers, paths, script locations and other information. Earlier versions of PHP used the $HTTP_server_VARS array, which is now deprecated. The information of $_SERVER on different servers is not necessarily the same. Common usage is as follows:

  1. echo $_SERVER['SERVER_ADDR'];
  2. echo $_SERVER['QUERY_STRING'];
Copy code

The more common way to get QUERY_STRING is through $_SERVER['QUERY_STRING '] What you get is something like name=mike&age=30. To convert values ​​in this format into variables, there is a method parse_str in PHP that can achieve this function. Official example:

  1. $str = "first=value&arr[]=foo+bar&arr[]=baz";
  2. parse_str($str);
  3. echo $first; // value
  4. echo $arr[ 0]; // foo bar
  5. echo $arr[1]; // baz
  6. parse_str($str, $output);
  7. echo $output['first']; // value
  8. echo $output['arr'] [0]; // foo bar
  9. echo $output['arr'][1]; // baz
  10. ?>
Copy the code

pass the code in the local environment:

  1. echo "";
  2. var_dump($_SERVER);
  3. echo "";
Copy code

用带格式的数据打印出数组内容:

array(31) { ["HTTP_ACCEPT"]=> string(3) "*/*" ["HTTP_ACCEPT_LANGUAGE"]=> string(5) "zh-CN" ["HTTP_USER_AGENT"]=> string(205) "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.2; Avant Browser)" ["HTTP_ACCEPT_ENCODING"]=> string(13) "gzip, deflate" ["HTTP_HOST"]=> string(14) "localhost:8080" ["HTTP_CONNECTION"]=> string(10) "Keep-Alive" ["HTTP_COOKIE"]=> string(154) "codehilite=IsPre=True&IsShowRowNumber=False&IsUseBR=False&Language=Csharp; iGHA2Cas=key=s8hoPBw6cWYHJ34NowHt%2f77gsEiQ9U9K0dDGPYjwLCFzQbqnNjlYMnUw9OOCF68u" ["PATH"]=> string(540) "C:Program Files (x86)ActiveState Komodo IDE 7;C:Program Files (x86)ActiveState Komodo Edit 7;E:appAdministratorproduct11.1.0client_1bin;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:Program Files (x86)Microsoft SQL Server100ToolsBinn;C:Program FilesMicrosoft SQL Server100ToolsBinn;C:Program FilesMicrosoft SQL Server100DTSBinn;C:Program FilesTortoiseSVNbin;C:Program Files (x86)Microsoft asp.netASP.NET Web Pagesv1.0;d:php-5.4.4-Win32-VC9-x86;" ["SystemRoot"]=> string(10) "C:Windows" ["COMSPEC"]=> string(27) "C:Windowssystem32cmd.exe" ["PATHEXT"]=> string(53) ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC" ["WINDIR"]=> string(10) "C:Windows" ["SERVER_SIGNATURE"]=> string(0) "" ["SERVER_SOFTWARE"]=> string(31) "Apache/2.2.22 (Win32) PHP/5.4.4" ["SERVER_NAME"]=> string(9) "localhost" ["SERVER_ADDR"]=> string(9) "127.0.0.1" ["SERVER_PORT"]=> string(4) "8080" ["REMOTE_ADDR"]=> string(9) "127.0.0.1" ["DOCUMENT_ROOT"]=> string(13) "D:/phpwwwroot" ["SERVER_ADMIN"]=> string(11) "dds@wwd.com" ["SCRIPT_FILENAME"]=> string(29) "D:/phpwwwroot/RecJP/test2.php" ["REMOTE_PORT"]=> string(5) "23827" ["GATEWAY_INTERFACE"]=> string(7) "CGI/1.1" ["SERVER_PROTOCOL"]=> string(8) "HTTP/1.1" ["REQUEST_METHOD"]=> string(3) "GET" ["QUERY_STRING"]=> string(0) "" ["REQUEST_URI"]=> string(16) "/RecJP/test2.php" ["SCRIPT_NAME"]=> string(16) "/RecJP/test2.php" ["PHP_SELF"]=> string(16) "/RecJP/test2.php" ["REQUEST_TIME_FLOAT"]=> float(1351577790572) ["REQUEST_TIME"]=> int(-1336907668) }


Statement:
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