PHP gets the full URL address of the current page, PHP gets the current url_php tutorial

Source: Internet
Author: User

PHP Gets the current page full URL address, PHP gets the current URL


When using PHP to write programs, we often want to get the URL of the current page. Here is a function to get the URL of the current page and how to use it:
Example one:

<?php//Description: Get the full urlfunction Curpageurl () {  $pageURL = ' http ';  if ($_server["HTTPS"] = = "on")   {    $pageURL. = "S";  }  $pageURL. = "://";  if ($_server["Server_port"]! = "+")   {    $pageURL. = $_server["SERVER_NAME"]. ":" . $_server["Server_port"]. $_server["Request_uri"];  }   else   {    $pageURL. = $_server["SERVER_NAME"]. $_server["Request_uri"];  }  return $pageURL;}? >

Once you have defined the function, you can call it directly:

<?php echo Curpageurl ();? >

The above function can get the full URL of the current page, which is what you see in the browser's address bar. However, sometimes we do not want the parameters in the URL (? , such as: http://www.ludou.org/hello.html?u=123, just want to get http://www.ludou.org/hello.html, you can modify the above function as example two.

Example two:

<?php//Description: Get no parameter urlfunction Curpageurl () {  $pageURL = ' http ';  if ($_server["HTTPS"] = = "on")   {    $pageURL. = "S";  }  $pageURL. = "://";  $this _page = $_server["Request_uri"];    Take only? The preceding content  if (Strpos ($this _page, "?")!== false)  {    $this _pages = Explode ("?", $this _page);    $this _page = reset ($this _pages);  }  if ($_server["Server_port"]! = "+")   {    $pageURL. = $_server["SERVER_NAME"]. ":" . $_server["Server_port"]. $this _page;  }   else   {    $pageURL. = $_server["SERVER_NAME"]. $this _page;  }  return $pageURL;}? >

Of course you can also use $_server[' php_self ', which does not return parameters in the URL.

Example three:

<?php//Description: Get no parameter urlfunction Curpageurl () {  $pageURL = ' http ';  if ($_server["HTTPS"] = = "on")   {    $pageURL. = "S";  }  $pageURL. = "://";  if ($_server["Server_port"]! = "a")   {    $pageURL. = $_server["SERVER_NAME"]. ":". $_server["Server_port"]. $_server[' php_self ');  }   else   {    $pageURL. = $_server["SERVER_NAME"]. $_server[' php_self '];  }  return $pageURL;}? >

In addition, $_server[' Request_uri ' and $_server[' Request_url '] are slightly different:
$_server["Request_uri"] returns the full path, including the parameter (/directory/file.ext?query=string)
$_server[' Request_url ' only returns the file path, excluding parameters, (/directory/file.ext), and $_server[' php_self '), except on some servers $_ server[' Request_url '] not available!

Note: URLs using rewrite rules, $_server[' php_self ' and $_server[' Request_url ' may not return what you want

Finally, to remind you that $_server["Request_uri" is only supported by Apache and want to get $_server[' Request_uri ') values, you can use the following scenarios:

<?php//Description: Gets the universal solution for _server[' Request_uri '] value function Request_uri () {  if (isset ($_server[' Request_uri '))  {    $uri = $_server[' Request_uri ');   }  else  {    if (isset ($_server[' argv '))    {      $uri = $_server[' php_self ']. '? '. $_server[' argv '][0];    }    else    {      $uri = $_server[' php_self ']. '? '. $_server[' query_string ';    }  }  return $uri;}? >

And then we'll share two solutions:

The first method:

<?php/** * Get the current page full URL address */function get_url () {  $sys _protocal = isset ($_server[' Server_port ']) && $_ server[' server_port '] = = ' 443 '? ' https://': '/http ';  $php _self = $_server[' php_self ')? $_server[' php_self ': $_server[' script_name '];  $path _info = isset ($_server[' path_info ')? $_server[' path_info ': ';  $relate _url = isset ($_server[' Request_uri ')? $_server[' Request_uri ': $php _self. (Isset ($_server[' query_string '])? '?'. $_server[' query_string ': $path _info);  Return $sys _protocal. (Isset ($_server[' http_host '))? $_server[' Http_host ']: '). $relate _url;} Echo Get_url ();? >

The second method:

    • JavaScript implementation
Top.location.href  The address of the top-level window this.location.href the address of the current window
    • PHP implementation

#测试网址:   http://localhost/blog/testurl.php?id=5//get the domain name or host address echo $_server[' http_host '].
"; #localhost//Get web address echo $_server[' php_self ']. "
"; #/blog/testurl.php//get URL parameters echo $_server["Query_string"]. "
"; #id =5//Get the user agent echo $_server[' Http_referer '].
"; Get the full Urlecho ' http://'. $_server[' http_host '].$_server[' request_uri '];echo '/http '. $_server[' Http_host '].$_ server[' php_self ']. '? '. $_server[' query_string '; #http://localhost/blog/testurl.php?id=5//contains the port number of the complete Urlecho ' http://'. $_server[' Server_ NAME ']. ': '. $_server["Server_port"].$_server["Request_uri"]; #http://localhost:80/blog/testurl.php?id=5//only take path $url= '/http '. $_server[' server_name '].$_server["Request_uri"] ; echo dirname ($url); #http://localhost/blog

I hope this article will help you learn PHP programming.

Articles you may be interested in:

    • PHP gets the full URL address
    • PHP URL address get function code (port, etc.) recommended
    • PHP Preset HTTP String function code for the URL address obtained by the form
    • Get an email address based on PHP curl
    • PHP uses curl to get the address instance after 302 jumps
    • PHP gets the current full URL address of the function

http://www.bkjia.com/PHPjc/1086646.html www.bkjia.com true http://www.bkjia.com/PHPjc/1086646.html techarticle PHP Gets the current page full URL address, PHP gets the current URL when using PHP to write programs, we often want to get the URL of the current page. The following provides an access to the current page ur ...

  • 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.