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:
- // BaseUrl
- Function baseUrl ($ uri = ''){
- $ BaseUrl = (isset ($ _ SERVER ['https']) & $ _ SERVER ['https']! = 'Off ')? 'Https: // ': 'http ://';
- $ BaseUrl. = isset ($ _ SERVER ['http _ host'])? $ _ SERVER ['http _ host']: getenv ('http _ host ');
- $ BaseUrl. = isset ($ _ SERVER ['script _ name'])? Dirname ($ _ SERVER ['script _ name']): dirname (getenv ('script _ name '));
- Return $ baseUrl. '/'. $ uri;
- }
Method 2 code:
- /**
- * Suppose, you are browsing in your localhost
- * Http: // localhost/myproject/index. php? Id = 8
- */
- Function baseUrl ()
- {
- // Output:/myproject/index. php
- $ CurrentPath = $ _ SERVER ['php _ SELF '];
- // Output: Array ([dirname] =>/myproject [basename] => index. php [extension] => php [filename] => index)
- $ PathInfo = pathinfo ($ currentPath );
- // Output: localhost
- $ HostName = $ _ SERVER ['http _ host'];
- // Output: http ://
- $ Protocol = strtolower (substr ($ _ SERVER ["SERVER_PROTOCOL"], 0, 5) = 'https ://'? 'Https: // ': 'http ://';
- // Return: http: // localhost/myproject/
- Return $ protocol. $ hostName. $ pathInfo ['dirname']. "/";
- }
Method 3 code:
-
- /**
- * @ Author mckee
- * @ Blog http://www.phpfensi.com
- */
- Function get_page_url (){
- $ Url = (isset ($ _ SERVER ['server _ port']) & $ _ SERVER ['server _ port'] = '000000 ')? 'Https: // ': 'http ://';
- $ Url. = $ _ SERVER ['http _ host'];
- $ Url. = isset ($ _ SERVER ['request _ URI '])? $ _ SERVER ['request _ URI ']: urlencode ($ _ SERVER ['php _ SELF']). '? '. Urlencode ($ _ SERVER ['query _ string']);
- Return $ url;
- }
- Echo get_page_url ();
- ?>
The following describes how to obtain the complete path of the current page. the code is as follows:
-
- Function getFullUrl (){
- # Solving common problems
- $ RequestUri = '';
- If (isset ($ _ SERVER ['request _ URI ']) {# $ _ SERVER ["REQUEST_URI"] is only supported by apache,
- $ RequestUri = $ _ SERVER ['request _ URI '];
- } Else {
- If (isset ($ _ SERVER ['argv']) {
- $ RequestUri = $ _ SERVER ['php _ SELF '].'? '. $ _ SERVER ['argv'] [0];
- } Else if (isset ($ _ SERVER ['query _ string']) {
- $ RequestUri = $ _ SERVER ['php _ SELF '].'? '. $ _ SERVER ['query _ string'];
- }
- }
- // Echo $ requestUri .' ';
- $ Scheme = emptyempty ($ _ SERVER ["HTTPS"])? '': ($ _ SERVER [" HTTPS "] =" on ")? "S ":"";
- $ Protocol = strstr (strtolower ($ _ SERVER ["SERVER_PROTOCOL"]), "/", true). $ scheme;
- $ Port = ($ _ SERVER ["SERVER_PORT"] = "80 ")? "": (":". $ _ SERVER ["SERVER_PORT"]);
- # Obtain the complete url
- $ _ FullUrl = $ protocol. ": //". $ _ SERVER ['server _ name']. $ port. $ requestUri;
- Return $ _ fullUrl;
- }
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.