Php obtains the complete URL of the current page, and php obtains the current url_PHP tutorial.

Source: Internet
Author: User
Tags learn php programming
Php obtains the complete URL of the current page, and php obtains the current url. Php gets the complete URL of the current page. When php gets the current url and uses PHP to write programs, we often want to get the URL of the current page. The following provides a URL for getting the current page UR php to get the complete url of the current page, php to get the current URL

When writing a program using PHP, we often want to obtain the URL of the current page. The following provides a function for obtaining the URL of the current page and its usage:
Example 1:

<? Php // description: obtain the complete URLfunction curPageURL () {$ pageURL = 'http'; if ($ _ SERVER ["HTTPS"] = "on") {$ pageURL. = "s" ;}$ pageURL. = ": //"; if ($ _ SERVER ["SERVER_PORT"]! = "80") {$ pageURL. = $ _ SERVER ["SERVER_NAME"]. ":". $ _ SERVER ["SERVER_PORT"]. $ _ SERVER ["REQUEST_URI"];} else {$ pageURL. = $ _ SERVER ["SERVER_NAME"]. $ _ SERVER ["REQUEST_URI"];} return $ pageURL;}?>

After defining this function, you can directly call it:

<?php echo curPageURL();?>

The above function can get the complete URL of the current page, that is, the content you see in the browser address bar. However, sometimes we do not want parameters in the URL (? Number), such as: http://www.ludou.org/hello.html? U = 123. you only want to get http://www.ludou.org/hello.html. you can modify the above parameters by using the example 2.

Example 2:

<? Php // description: obtain URLfunction curPageURL () {$ pageURL = 'http'; if ($ _ SERVER ["HTTPS"] = "on") {$ pageURL. = "s" ;}$ pageURL. = ": //"; $ this_page = $ _ SERVER ["REQUEST_URI"]; // get only? The preceding content, if (strpos ($ this_page ,"? ")! = False) {$ this_pages = explode ("? ", $ This_page); $ this_page = reset ($ this_pages);} if ($ _ SERVER [" SERVER_PORT "]! = "80") {$ 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 '] (this variable does not return parameters in the URL ),

Example 3:

<? Php // description: obtain URLfunction curPageURL () {$ pageURL = 'http'; if ($ _ SERVER ["HTTPS"] = "on") {$ pageURL. = "s" ;}$ pageURL. = ": //"; if ($ _ SERVER ["SERVER_PORT"]! = "80") {$ pageURL. = $ _ SERVER ["SERVER_NAME"]. ":". $ _ SERVER ["SERVER_PORT"]. $ _ SERVER ['php _ SELF '];} else {$ pageURL. = $ _ SERVER ["SERVER_NAME"]. $ _ SERVER ['php _ SELF '];} return $ pageURL;}?>

In addition, $ _ SERVER ['request _ url'] and $ _ SERVER ['request _ url'] are slightly different:
$ _ SERVER ["REQUEST_URI"]Return the complete path, including the parameter (/directory/file. ext? Query = string)
$ _ SERVER ['request _ url']Only the file path is returned, excluding parameters (/directory/file. ext), similar to $ _ SERVER ['php _ SELF '], except that $ _ SERVER ['request _ url'] is unavailable on some servers!

Note: when a URL uses a rewrite rule, $ _ SERVER ['php _ SELF '] and $ _ SERVER ["REQUEST_URL"] may not return what you want.

Note that $ _ SERVER ["REQUEST_URI"] is only supported by apache. to obtain the value of $ _ SERVER ['request _ URI '], you can use the following solution:

<? Php // description: obtain the value of _ SERVER ['request _ url']. 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 ;}?>

We will share with you two solutions:

Method 1:

<? Php/*** get the complete URL of the current page */function get_url () {$ sys_protocal = isset ($ _ SERVER ['server _ port']) & $ _ SERVER ['server _ port'] = '000000 '? '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 ();?>

Method 2:

  • Javascript implementation
Top. location. href top-level window address this. location. href current window address
  • PHP implementation

# Test URL: http: // localhost/blog/testurl. php? Id = 5 // Obtain the domain name or HOST address echo $ _ SERVER ['http _ host']."
"; # Localhost // Obtain the webpage address echo $ _ SERVER ['php _ SELF ']."
"; #/Blog/testurl. php // Obtain the url parameter echo $ _ SERVER [" QUERY_STRING "]."
"; # Id = 5 // obtain the user proxy echo $ _ SERVER ['http _ referer']."
"; // Obtain the complete 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 // Complete urlecho 'http: // 'containing the port number ://'. $ _ SERVER ['server _ name']. ':'. $ _ SERVER ["SERVER_PORT"]. $ _ SERVER ["REQUEST_URI"]; # http: // localhost: 80/blog/testurl. php? Id = 5 // only the 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 obtains the complete url
  • Php url to obtain function code (port, etc.) recommended
  • PHP presets the http string function code for the URL address obtained from the form
  • Php curl-based mailbox address retrieval
  • PHP uses CURL to obtain the address instance after 302 redirection
  • Function of PHP to obtain the current complete URL address

When using PHP to write a program, ghost often wants to obtain the URL of the current page. The following provides a url for obtaining 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.