Php obtains the complete url of the current page.

Source: Internet
Author: User
In php, we need to use several common php global variable functions to obtain the complete url of the current page. these variables are mainly $ _ SERVER, next, let me show you how to get an image

In php, we need to use several common php global variable functions to obtain the complete url of the current page. these variables are mainly $ _ SERVER, next I will show you a complete url program for getting the current page.

Let's take a look at some PHP variables:

$ _ SERVER ['server _ name'] # NAME of the SERVER host where the script is currently running.

$ _ SERVER ['query _ string'] # query string.

$ _ SERVER ['http _ host'] # HOST of the current request: content in the header.

$ _ SERVER ['http _ referer'] # Link to the URL of the previous page of the current page.

$ _ SERVER ['server _ port'] # PORT used by the SERVER

$ _ SERVER ['request _ URI '] # URI required to access this page.

With some surface functions, we can start. First, let's look at some base methods.

Two methods of baseUrl

Method 1 code:

  1. // BaseUrl
  2. Function baseUrl ($ uri = ''){
  3. $ BaseUrl = (isset ($ _ SERVER ['https']) & $ _ SERVER ['https']! = 'Off ')? 'Https: // ': 'http ://';
  4. $ BaseUrl. = isset ($ _ SERVER ['http _ host'])? $ _ SERVER ['http _ host']: getenv ('http _ host ');
  5. $ BaseUrl. = isset ($ _ SERVER ['script _ name'])? Dirname ($ _ SERVER ['script _ name']): dirname (getenv ('script _ name '));
  6. Return $ baseUrl. '/'. $ uri;
  7. }

Method 2 code:

  1. /**
  2. * Suppose, you are browsing in your localhost
  3. * Http: // localhost/myproject/index. php? Id = 8
  4. */
  5. Function baseUrl ()
  6. {
  7. // Output:/myproject/index. php
  8. $ CurrentPath = $ _ SERVER ['php _ SELF '];
  9. // Output: Array ([dirname] =>/myproject [basename] => index. php [extension] => php [filename] => index)
  10. $ PathInfo = pathinfo ($ currentPath );
  11. // Output: localhost
  12. $ HostName = $ _ SERVER ['http _ host'];
  13. // Output: http ://
  14. $ Protocol = strtolower (substr ($ _ SERVER ["SERVER_PROTOCOL"], 0, 5) = 'https ://'? 'Https: // ': 'http ://';
  15. // Return: http: // localhost/myproject/
  16. Return $ protocol. $ hostName. $ pathInfo ['dirname']. "/";
  17. }

Method 3 code:

  1. /**
  2. * @ Author mckee
  3. * @ Blog http://www.phpfensi.com
  4. */
  5. Function get_page_url (){
  6. $ Url = (isset ($ _ SERVER ['server _ port']) & $ _ SERVER ['server _ port'] = '000000 ')? 'Https: // ': 'http ://';
  7. $ Url. = $ _ SERVER ['http _ host'];
  8. $ Url. = isset ($ _ SERVER ['request _ URI '])? $ _ SERVER ['request _ URI ']: urlencode ($ _ SERVER ['php _ SELF']). '? '. Urlencode ($ _ SERVER ['query _ string']);
  9. Return $ url;
  10. }
  11. Echo get_page_url ();
  12. ?>

The following describes how to obtain the complete path of the current page. the code is as follows:

  1. Function getFullUrl (){
  2. # Solving common problems
  3. $ RequestUri = '';
  4. If (isset ($ _ SERVER ['request _ URI ']) {# $ _ SERVER ["REQUEST_URI"] is only supported by apache,
  5. $ RequestUri = $ _ SERVER ['request _ URI '];
  6. } Else {
  7. If (isset ($ _ SERVER ['argv']) {
  8. $ RequestUri = $ _ SERVER ['php _ SELF '].'? '. $ _ SERVER ['argv'] [0];
  9. } Else if (isset ($ _ SERVER ['query _ string']) {
  10. $ RequestUri = $ _ SERVER ['php _ SELF '].'? '. $ _ SERVER ['query _ string'];
  11. }
  12. }
  13. // Echo $ requestUri .' ';
  14. $ Scheme = emptyempty ($ _ SERVER ["HTTPS"])? '': ($ _ SERVER [" HTTPS "] =" on ")? "S ":"";
  15. $ Protocol = strstr (strtolower ($ _ SERVER ["SERVER_PROTOCOL"]), "/", true). $ scheme;
  16. $ Port = ($ _ SERVER ["SERVER_PORT"] = "80 ")? "": (":". $ _ SERVER ["SERVER_PORT"]);
  17. # Obtain the complete url
  18. $ _ FullUrl = $ protocol. ": //". $ _ SERVER ['server _ name']. $ port. $ requestUri;
  19. Return $ _ fullUrl;
  20. }

Echo getFullUrl (); Note: Because php does not have built-in functions, we need to combine the parameters on the url to implement the whole url.

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.