Php obtains the php server variable in the current url path.

Source: Internet
Author: User
Php obtains the php server variable in the current url path.

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:

  1. // Process request_uri

  2. If (! Isset ($ _ server ['request _ uri ']) {
  3. $ _ Server ['request _ uri '] = $ _ server ['php _ self'];
  4. If (isset ($ _ server ['query _ string']) $ _ server ['request _ uri ']. = '? '. $ _ Server ['query _ string'];
  5. }
  6. If ($ _ server ['request _ uri ']) {
  7. $ Temp = urldecode ($ _ server ['request _ uri ']);
  8. If (strexists ($ temp, '<') | strexists ($ temp ,'"')){
  9. $ _ Get = shtmlspecialchars ($ _ get); // xss
  10. }
  11. }

  12. Echo $ _ server ['document _ root']."
    "; // Get the root variable of the server document

  13. Echo $ _ server ['php _ self ']."
    "; // Get the absolute path variable of the file server that executes the code
  14. Echo _ file __."
    "; // Get the absolute path variable of the file system
  15. Echo dirname (_ file _); // function used to obtain the path of the folder where the file is located
  16. ?>
  17. // Server function
  18. $ _ Server ["http_referer"] = http: // localhost/lianxi/
  19. $ _ Server ["http_accept_language"] = zh-cn
  20. $ _ Server ["http_accept_encoding"] = gzip, deflate
  21. $ _ 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)
  22. $ _ Server ["http_host"] = localhost
  23. $ _ Server ["http_connection"] = keep-alive
  24. $ _ 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
  25. $ _ Server ["systemroot"] = c: \ windows
  26. $ _ Server ["comspec"] = c: \ windows \ system32 \ cmd.exe
  27. $ _ Server ["pathext" cmd).comcmd.exe;. bat;. cmd;. vbs;. vbe;. js;. jse;. wsf;. wsh
  28. $ _ Server ["windir"] = c: \ windows
  29. $ _ Server ["server_signature"] =
  30. Apache/2.0.55 (win32) php/5.1.1 server at localhost port 80 \ which server is used
  31. $ _ Server ["server_software"] = apache/2.0.55 (win32) php/5.1.1
  32. $ _ Server ["server_name"] = localhost \ server name
  33. $ _ Server ["server_addr"] = 127.0.0.1
  34. $ _ Server ["server_port"] = 80 \ server port
  35. $ _ Server ["remote_addr"] = 127.0.0.1
  36. $ _ Server ["document_root"] = d:/lianxi \ main directory of the website
  37. $ _ Server ["server_admin"] = sss@163.com \ mailbox set when apache is installed
  38. $ _ Server ["script_filename"] = d:/lianxi/servervalues. php \ absolute path of the current webpage,
  39. $ _ Server ["remote_port"] = 1076 \ remote port
  40. $ _ Server ["gateway_interface"] = cgi/1.1
  41. $ _ Server ["server_protocol"] = http/1.1
  42. $ _ Server ["request_method"] = get
  43. $ _ Server ["query_string"] =\\ get? Content after No.
  44. $ _ Server ["request_uri"] = Example:/lianxi/servervalues. php? A = 1 & B = 2
  45. $ _ Server ["script_name"] = Example:/lianxi/servervalues. php
  46. $ _ Server ["php_self"] =/lianxi/servervalues. php \ returns the relative path of the current webpage.
  47. $ _ Server ["request_time"] = 1179190013 \ the unit of running time is 100,000 in milliseconds
  48. $ _ Server ["argv"] = array
  49. $ _ Server ["argc"] = 0

Example 2:

  1. /**
  2. _ File __,
  3. Getcwd (),
  4. $ _ Server ["request_uri"],
  5. $ _ Server ["script_name"],
  6. $ _ Server ["php_self"],
  7. $ _ 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:

  1. Echo "test1.php variables
    ";
  2. Echo "getcwd:", getcwd (),"
    ";
  3. Echo "_ file __:", _ file __,"
    ";
  4. Echo "request_uri:", $ _ server ["request_uri"],"
    ";
  5. Echo "script_name:", $ _ server ["script_name"],"
    ";
  6. Echo "php_self:", $ _ server ["php_self"],"
    ";
  7. Echo "script_filename", $ _ server ["script_filename"],"
    ";
  8. // Include test2.php and output the above variables in test2.php. what is the difference:
  9. Include_once ("test2/test2.php ");
  10. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.