Programming in PHP, get the current URL address, and server variables, mainly using the following global variables: $_server["query_string"],$_server["Request_uri"],$_server["Script_name" ],$_server["Php_self"] 1,$_server["query_string") Description: Query string 2,$_server["Request_uri"] Description: The uri3,$_server["Script_name" required to access this page Description: Contains the path of the current script 4,$_server["php_self" description: The file name instance of the currently executing script: 1,http://bbs.it-home.org/(open home page directly) results: $_server["query_string "] =" "$_server[" request_uri "] ="/"$_server[" script_name "] ="/index.php "$_server[" php_self "] ="/index.php "2,http:// bbs.it-home.org/?p=222 (with Query) results: $_server["query_string"] = "p=222" $_server["request_uri"] = "/?p=222" $_server[" Script_name "] ="/index.php "$_server[" php_self "] ="/index.php "3,http://bbs.it-home.org/index.php?p=222&q= BIUUU results:
Handling Request_uri
- if (!isset ($_server[' Request_uri ')) {
- $_server[' Request_uri '] = $_server[' php_self '];
- if (Isset ($_server[' query_string ')) $_server[' Request_uri ']. = '? '. $_server[' query_string '];
- }
- if ($_server[' Request_uri ']) {
- $temp = UrlDecode ($_server[' Request_uri ');
- if (strexists ($temp, ' < ') | | strexists ($temp, ' "')) {
- $_get = Shtmlspecialchars ($_get);//XSS
- }
- }
echo $_server[' Document_root ']. " "; Get Server Document root variable
- echo $_server[' php_self ']. "
"; Gets the variable that executes the file server absolute path to the code
- echo __file__. "
"; Gets the file system absolute path of the variable
- echo dirname (__file__); function to get the folder path where the file is located
- ?>
- Server functions
- $_server["Http_referer"]=http://localhost/lianxi/
- $_server["Http_accept_language"]=ZH-CN
- $_server["Http_accept_encoding"]=gzip, deflate
- $_server["Http_user_agent"]=mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;. NET CLR 1.1.4322;. NET CLR 2.0.50727)
- $_server["Http_host"]=localhost
- $_server["Http_connection"]=keep-alive
- $_server["path"]=c:\windows\system32;c:\windows;c:\windows\system32\wbem;c:\program Files\Common Files\adobe\agl ; C:\Program Files\mysql\mysql Server 5.0\bin;c:\php;c:\php\ext
- $_server["systemroot"]=c:\windows
- $_server["ComSpec"]=c:\windows\system32\cmd.exe
- $_server["Pathext"]=.com;. EXE;. BAT;. cmd;. VBS;. VBE;. JS;. JSE;. WSF;. Wsh
- $_server["windir"]=c:\windows
- $_server["Server_signature"]=
- apache/2.0.55 (Win32) php/5.1.1 server at localhost port 80 \ \ What servers to use
- $_server["Server_software"]=apache/2.0.55 (Win32) php/5.1.1
- $_server["SERVER_NAME"]=localhost \ \ server Name
- $_server["Server_addr"]=127.0.0.1
- $_server["Server_port"]=80 \ \ Server Port
- $_server["REMOTE_ADDR"]=127.0.0.1
- $_server["Document_root"]=d:/lianxi \ \ site's home directory
- $_server["server_admin"]=sss@163.com \ \ e-mail set when installing Apache
- $_server["Script_filename"]=d:/lianxi/lianxi/servervalues.php \ \ The absolute path of the current page,
- $_server["Remote_port"]=1076 \ \ Remote Port
- $_server["Gateway_interface"]=cgi/1.1
- $_server["Server_protocol"]=http/1.1
- $_server["Request_method"]=get
- $_server["Query_string"]=\\ get? The contents after the number
- $_server["Request_uri"]= Example:/lianxi/servervalues.php?a=1&b=2
- $_server["Script_name"]= Example:/lianxi/servervalues.php
- $_server["php_self"]=/lianxi/servervalues.php \ \ Returns the relative path of the current page.
- $_server["Request_time"]=1179190013 \ \ Run time unit is one out of 10,000 milliseconds
- $_server["argv"]=array
- $_server["ARGC"]=0
Copy CodeExample 2:
- /**
- __FILE__,
- GETCWD (),
- $_server["Request_uri"],
- $_server["Script_name"],
- $_server["Php_self"],
- $_server["Script_filename"],
Copy CodeThe similarities and differences between these variables or functions. Suppose there is a request address: http://localhost:8080/test.php/age=20 and the full path to test.php is: d:/server/www/example/test.php 1), GETCWD () The directory where the page file where the browser request will be obtained. The directory where the test.php file is located: d:/server/www/example/, if require or include statements are executed in test.php, such as Inculde ("test_dir/test2.php"), then the Te The GETCWD () function returned by the st2.php will also be the directory where the test.php is located. 2), __file__ a magic variable, with it will get the __file__ variable where the full path of the file, such as: test.php __file__ will get d:/server/www/example/test.php, test_dir/test 2.php __file__ will get d:/server/www/example/test_dir/test2.php 3), $_server["Script_filename"] will get the full path of the page file requested by the browser. test.php and test_dir/test2.php $_server["Script_name" will be d:/server/www/example/test.php. 4), $_server["Script_name"] will get the file name of the paging file requested by the browser, note: Unlike $_server["Script_name", this variable only gets the file name without the path, in test.php and test_dir/ test2.php with $_server["Script_name" will be test.php. Of course, the test.php and test_dir/test2.php execute basename ($_server["Script_filename") are the same as $_server["Script_name". Perform Realpath ("test.php") in test.php with test_dir/test2.php to achieve results with $_server["Script_fileName "] is the same. 5), $_server["Php_self"] will get the file name of the browser request page, and peel off the question mark? After the content, note: Do not include the path, such as in the client request Http://localhost:8080/test.php?age=20&name=tom, then test.php and test_dir/test2.php $_ server["Php_self" will get "test.php". "Age=20&name=tom" was stripped off. And if the client requests Http://localhost:8080/test.php/age=20&name=tom, then test.php and test_dir/test2.php $_server["Php_self"] Will be "Test.php/age=20&name=tom". 6), $_server["Request_uri"] will get the file name of the browser request page, and all the contents after the file name (note: The contents after the pound # will be omitted), such as in the client request http://localhost:8080/test.php Age=20&name=tom, then test.php and test_dir/test2.php $_server["Reuest_uri" will get "test.php". "Age=20&name=tom" was stripped off. And if the client requests Http://localhost:8080/test.php/age=20&name=tom, then test.php and test_dir/test2.php $_server["Request_ Uri "] will be" Test.php/age=20&name=tom ". test.php:
- echo "test1.php variables
”;
- echo "GETCWD:", GETCWD (), "
”;
- echo "__file__:", __file__, "
”;
- echo "Request_uri:", $_server["Request_uri"], "
”;
- echo "Script_name:", $_server["Script_name"], "
”;
- echo "Php_self:", $_server["php_self"], "
”;
- echo "Script_filename", $_server["Script_filename"], "
”;
- Include test2.php, and output the above variables in test2.php to see what's different:
- Include_once ("test2/test2.php");
- ?>
Copy Code |