PHP retrieves the complete URL of the current page

Source: Internet
Author: User
Tags http authentication server array
PHP obtains the complete URL of the current page through the pre-defined variable $ _ SERVER. $ _ SERVER is an array containing header information, path, and file location, which is created by the web SERVER, if you use the command line to run PHP, then $ _ SERVER is completely

PHP obtains the complete URL of the current page through the pre-defined variable $ _ SERVER. $ _ SERVER is an array containing header information, path, and file location, which is created by the web SERVER, if you use the command line to run PHP, $ _ SERVER is completely invalid.

Description of elements in the $ _ SERVER array:


  1. $ _ SERVER ['php _ SELF '] # file name of the script being executed, which is related to document root.
  2. $ _ SERVER ['argv'] # parameters passed to the script.
  3. $ _ SERVER ['argc '] # contains the number of command line parameters passed to the program (if running in command line mode ).
  4. $ _ SERVER ['gateway _ interface'] # The CGI standard version used by the SERVER. For example, "CGI/1.1 ".
  5. $ _ SERVER ['server _ name'] # NAME of the SERVER host where the script is currently running.
  6. $ _ SERVER ['server _ soft'] # The string identified by the SERVER, which is provided in the header of the response request.
  7. $ _ SERVER ['server _ protocol'] # name and version of the communication PROTOCOL used to request the page. For example, "HTTP/1.0 ".
  8. $ _ SERVER ['request _ method'] # request method for accessing the page. For example, "GET", "HEAD", "POST", and "PUT ".
  9. $ _ SERVER ['query _ string'] # query string. 
  10. $ _ SERVER ['document _ root'] # ROOT directory of the DOCUMENT where the script is currently running. Defined in the server configuration file.
  11. $ _ SERVER ['http _ ACCEPT '] # Accept of the current request: content in the header.
  12. $ _ SERVER ['http _ ACCEPT_CHARSET '] # Accept-Charset of the current request: content in the header. For example, "iso-8859-1, *, utf-8 ".
  13. $ _ SERVER ['http _ ACCEPT_ENCODING '] # Accept-Encoding of the current request: content in the header. For example, "gzip ".
  14. $ _ SERVER ['http _ ACCEPT_LANGUAGE '] # Accept-Language of the current request: content in the header. For example, "en ".
  15. $ _ SERVER ['http _ connection'] # CONNECTION of the current request: content in the header. For example, "Keep-Alive ".
  16. $ _ SERVER ['http _ host'] # HOST of the current request: content in the header.
  17. $ _ SERVER ['http _ referer'] # Link to the URL of the previous page of the current page. 
  18. $ _ SERVER ['http _ USER_AGENT '] # User_Agent of the current request: content in the header.
  19. $ _ SERVER ['https']-if accessed through HTTPS, it is set to a non-null value (on); otherwise, off is returned.
  20. $ _ SERVER ['remote _ ADDR '] # The IP address of the user on the current page is being viewed.
  21. $ _ SERVER ['remote _ host'] # the HOST name of the user browsing the current page.
  22. $ _ SERVER ['remote _ port'] # The PORT used by the user to connect to the SERVER.
  23. $ _ SERVER ['script _ filename'] # The absolute path name of the currently executed SCRIPT.
  24. $ _ SERVER ['server _ admin'] # Administrator information
  25. $ _ SERVER ['server _ port'] # PORT used by the SERVER
  26. $ _ SERVER ['server _ SIGNATURE '] # a string containing the SERVER version and virtual host name.
  27. $ _ SERVER ['path _ TRANSLATED '] # Basic PATH of the file system where the current script is located (not the root directory of the document.
  28. $ _ SERVER ['script _ name'] # contains the path of the current SCRIPT. This is useful when the page needs to point to itself.
  29. $ _ SERVER ['request _ URI '] # URI required to access this page.
  30. $ _ SERVER ['php _ AUTH_USER '] # When PHP runs in the Apache module mode and uses the HTTP authentication function, this variable is the user name entered by the user.
  31. $ _ SERVER ['php _ AUTH_PW '] # When PHP runs in the Apache module mode and is using the HTTP authentication function, this variable is the password entered by the user.
  32. $ _ SERVER ['auth _ type'] # When PHP runs in the Apache module mode and is using the HTTP authentication function, this variable is the authentication TYPE.

Note: Differences between SERVER_NAME and HTTP_HOST (If port 80 is not used, HTTP_HOST = SERVER_NAME: SERVER_PORT )
Common elements are also the bold parts above. using them, you can easily obtain the complete url.
 

Note: $_SERVER [ 'REQUEST_URI' ] Only apache is used as the web server. if it does not exist, it should $_SERVER [ 'PHP_SELF' ]) .  '?'  . $_SERVER [ 'QUERY_STRING' Urlencode can be used to prevent malicious URLs.
PS: it is much easier to get a url using javascript:
《script》document.write(this.location.href);《script》

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.