PHP gets the current URL path PHP server variable

Source: Internet
Author: User
Tags php server

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:

  1. Handling 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 Server Document root variable

  13. echo $_server[' php_self ']. "
    "; Gets the variable that executes the file server absolute path to the code
  14. echo __file__. "
    "; Gets the file system absolute path of the variable
  15. echo dirname (__file__); function to get the folder path where the file is located
  16. ?>
  17. Server functions
  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;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
  25. $_server["systemroot"]=c:\windows
  26. $_server["ComSpec"]=c:\windows\system32\cmd.exe
  27. $_server["Pathext"]=.com;. 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 \ \ What servers to use
  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 \ \ site's home directory
  37. $_server["server_admin"]=sss@163.com \ \ e-mail set when installing Apache
  38. $_server["Script_filename"]=d:/lianxi/lianxi/servervalues.php \ \ The absolute path of the current page,
  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? The contents after the number
  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 page.
  47. $_server["Request_time"]=1179190013 \ \ Run time unit is one out of 10,000 milliseconds
  48. $_server["argv"]=array
  49. $_server["ARGC"]=0

Copy Code

Example 2:

    1. /**
    2. __FILE__,
    3. GETCWD (),
    4. $_server["Request_uri"],
    5. $_server["Script_name"],
    6. $_server["Php_self"],
    7. $_server["Script_filename"],
Copy Code

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

    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 to see what's different:
    9. Include_once ("test2/test2.php");
    10. ?>
Copy Code
  • 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.