Programming in php to obtain the current url address and server variables mainly uses 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: uri3, $ _ server ["script_name"] description: Path 4 containing the current script, $ _ server ["php_self"] description: file name instance of the script being executed: 1, http://bbs.it-home.org/(directly open the home page) 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) result: $ _ 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 result:
// Process 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 the root variable of the server document
- Echo $ _ server ['php _ self ']."
"; // Get the absolute path variable of the file server that executes the code
- Echo _ file __."
"; // Get the absolute path variable of the file system
- Echo dirname (_ file _); // function used to obtain the path of the folder where the file is located
- ?>
- // Server function
- $ _ 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 \ system32 \ wbem; c: \ program files \ common files \ adobe \ agl; c: \ program files \ mysql server 5.0 \ bin; c: \ php \ ext
- $ _ Server ["systemroot"] = c: \ windows
- $ _ Server ["comspec"] = c: \ windows \ system32 \ cmd.exe
- $ _ Server ["pathext" cmd).comcmd.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 \ which server is used
- $ _ 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 \ main directory of the website
- $ _ Server ["server_admin"] = sss@163.com \ mailbox set when apache is installed
- $ _ Server ["script_filename"] = d:/lianxi/servervalues. php \ absolute path of the current webpage,
- $ _ 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? Content after No.
- $ _ 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 webpage.
- $ _ Server ["request_time"] = 1179190013 \ the unit of running time is 100,000 in milliseconds
- $ _ Server ["argv"] = array
- $ _ Server ["argc"] = 0
Example 2:
- /**
- _ File __,
- Getcwd (),
- $ _ Server ["request_uri"],
- $ _ Server ["script_name"],
- $ _ Server ["php_self"],
- $ _ Server ["script_filename"],
-
Similarities and differences between these variables or functions. assume there is a request address: http://localhost:8080/test.php/age=20 Test. the complete php path is: d:/server/www/example/test. php 1), getcwd () will get the Directory of the page file requested by the browser. that is, test. directory where the php file is located: d:/server/www/example. php executes the require or include statement, such as inculde ("test_dir/test2.php"). in test2.php, the getcwd () function will return test. php directory. 2) use the _ file _ magic variable to obtain the complete path of the file where the _ file _ variable is located, such as test. in php, _ file _ will get d:/server/www/example/test. in php, test_dir/test2.php, _ file _ will get d:/server/ Www/example/test_dir/test2.php 3) and $ _ server ["script_filename"] get the complete path of the page file requested by the browser. test. use $ _ server ["script_name"] in php and test_dir/test2.php to get d:/server/www/example/test. php. 4) $ _ server ["script_name"] will get the file name of the page file requested by the browser. note: it is different from $ _ server ["script_name, this variable only obtains the file name and does not contain the path. php and test_dir/test2.php use $ _ server ["script_name"] to get test. php. of course, in test. php and test_dir/test2.php execute basename ($ _ server ["Script_filename"]) is the same as $ _ server ["script_name. run in test. php and test_dir/test2.php execute realpath ("test. the result is the same as $ _ server ["script_filename. 5) $ _ server ["php_self"] will get the file name of the browser request page and remove the question mark? After the content, note: does not contain the path, such as the request in the client http://localhost:8080/test.php?age=20&name=tom , The $ _ server ["php_self"] of test. php and test_dir/test2.php will get "test. php ". "Age = 20 & name = tom" is removed. 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 get "test. php/age = 20 & name = tom ". 6) $ _ server ["request_uri"] will get the file name of the browser request page, and all the content after the file name (note: the content after the # Well number will be omitted ), for example http://localhost:8080/test.php?age=20&name=tom , The $ _ server ["reuest_uri"] of test. php and test_dir/test2.php will get "test. php ". "Age = 20 & name = tom" is removed. 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 get "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. what is the difference:
- Include_once ("test2/test2.php ");
- ?>
|