PHP get current page full URL address _php tips

Source: Internet
Author: User
Tags php programming

When using PHP to write a program, we often want to get the URL of the current page. The following provides a function to get the current page URL and how to use it:
Example One:

<?php
//Description: Get full URL

function Curpageurl () 
{
  $pageURL = ' http ';

  if ($_server["HTTPS"] = = "on") 
  {
    $pageURL. = "S";
  }
  $pageURL. = "://";

  if ($_server["Server_port"]!= "O") 
  {
    $pageURL. = $_server["SERVER_NAME"]. ":" . $_server["Server_port"]. $_server["Request_uri"];
  } 
  else 
  {
    $pageURL. = $_server["SERVER_NAME"]. $_server["Request_uri"];
  return $pageURL;
>

After you define 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 change the above function by example two.

Example two:

<?php
//Description: Get parameter-free URL

function Curpageurl () 
{
  $pageURL = ' http ';

  if ($_server["HTTPS"] = = "on") 
  {
    $pageURL. = "S";
  }
  $pageURL. = "://";

  $this _page = $_server["Request_uri"];
  
  Just take it? The previous content
  if (Strpos ($this _page, "?")!== false)
  {
    $this _pages = Explode ("?", $this _page);
    $this _page = reset ($this _pages);
  }

  if ($_server["Server_port"]!= "O") 
  {
    $pageURL. = $_server["SERVER_NAME"]. ":" . $_server["Server_port"]. $this _page;
  } 
  else 
  {
    $pageURL. = $_server["SERVER_NAME"]. $this _page;
  }
  return $pageURL;
>

Of course, you can also use the $_server[' php_self ' (the variable does not return parameters in the URL),

Example three:

<?php
//Description: Get parameter-free URL

function Curpageurl () 
{
  $pageURL = ' http ';

  if ($_server["HTTPS"] = = "on") 
  {
    $pageURL. = "S";
  }
  $pageURL. = "://";

  if ($_server["Server_port"]!= "O") 
  {
    $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, containing the parameters (/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 ' is not available!

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

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

<?php
//Description: Get _server[' Request_uri ' value for generic solution
function Request_uri ()
{
  if (isset ' 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'll share two solutions for you:

The first method:

<?php
/**
 * Get 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 domain name or host address 
echo $_server[' Http_host ']. " <br> "; #localhost

//Get web address 
echo $_server[' php_self ']. " <br> "; #/blog/testurl.php

//Get URL parameter 
echo $_server["Query_string"]. " <br> "; #id =5

//Get user agent 
echo $_server[' Http_referer ']. " <br> "; 

Gets the full URL of
echo ' 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 full URL of the port number
echo ' http://'. $_server[' server_name '. $_ server["Server_port"].$_server["Request_uri"]; 
#http://localhost:80/blog/testurl.php?id=5

//Only fetch path
$url = ' http://'. $_server[' server_name '].$_server[' Request_uri "]; 
echo dirname ($url);
#http://localhost/blog

I hope this article will help you learn about PHP programming.

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.