To Get Full URL  in PHP:

In this post, We are discussing about How to get the full URL in PHP.

Have a look at:

[pastacode lang=”php” manual=”%24_SERVER%5B’REQUEST_URI’%5D%0A” message=”php code” highlight=”” provider=”manual”/] [ad type=”banner”] [pastacode lang=”javascript” manual=”%24actual_link%20%3D%20%22http%3A%2F%2F%24_SERVER%5BHTTP_HOST%5D%24_SERVER%5BREQUEST_URI%5D%22%3B%20%0A” message=”javascript code” highlight=”” provider=”manual”/]

Note: That the double quoted string syntax is perfectly correct

  • If we want to support both HTTP and HTTPS, we can use
[pastacode lang=”php” manual=”%24actual_link%20%3D%20(isset(%24_SERVER%5B’HTTPS’%5D)%20%3F%20%22https%22%20%3A%20%22http%22)%20.%20%22%3A%2F%2F%24_SERVER%5BHTTP_HOST%5D%24_SERVER%5BREQUEST_URI%5D%22%3B%20%0A” message=”php code” highlight=”” provider=”manual”/]
  • Using this code has security implications. The client can set HTTP_HOST and REQUEST_URI to any arbitrary value

  • Using a ternary statement:
[pastacode lang=”php” manual=”%24url%20%3D%20%22http%22%20.%20((%24_SERVER%5B’SERVER_PORT’%5D%20%3D%3D%20443)%20%3F%20%22s%3A%2F%2F%22%20%3A%20%22%3A%2F%2F%22)%20.%20%24_SERVER%5B’HTTP_HOST’%5D%20.%20%24_SERVER%5B’REQUEST_URI’%5D%3B%20%0A” message=”php code” highlight=”” provider=”manual”/] [ad type=”banner”]

  • Cross platform method for finding the current URL is:
[pastacode lang=”php” manual=”%24url%20%3D%20(isset(%24_SERVER%5B’HTTPS’%5D)%20%3F%20%22https%22%20%3A%20%22http%22)%20.%20%22%3A%2F%2F%24_SERVER%5BHTTP_HOST%5D%24_SERVER%5BREQUEST_URI%5D%22%3B%20%0A” message=”php code” highlight=”” provider=”manual”/]

Sample code:

[pastacode lang=”javascript” manual=”function%20full_path()%20%0A%24s%20%3D%20%26%24_SERVER%3B%0A%24ssl%20%3D%20(!empty(%24s%5B’HTTPS’%5D)%20%26%26%20%24s%5B’HTTPS’%5D%20%3D%3D%20’on’)%20%3F%20true%3Afalse%3B%20%0A%24sp%20%3D%20strtolower(%24s%5B’SERVER_PROTOCOL’%5D)%3B%0A%24protocol%20%3D%20substr(%24sp%2C%200%2C%20strpos(%24sp%2C%20’%2F’))%20.%20((%24ssl)%20%3F%20’s’%20%3A%20”)%3B%0A%24port%20%3D%20%24s%5B’SERVER_PORT’%5D%3B%20%0A%24port%20%3D%20((!%24ssl%20%26%26%20%24port%3D%3D’80’)%20%7C%7C%20(%24ssl%20%26%26%20%24port%3D%3D’443′))%20%3F%20”%20%3A%20’%3A’.%24port%3B%0A%24host%20%3D%20isset(%24s%5B’HTTP_X_FORWARDED_HOST’%5D)%20%3F%20%24s%5B’HTTP_X_FORWARDED_HOST’%5D%20%3A%20(isset(%24s%5B’HTTP_HOST’%5D)%20%3F%20%24s%5B’HTTP_HOST’%5D%20%3A%20null)%3B%0A%24host%20%3D%20isset(%24host)%20%3F%20%24host%20%3A%20%24s%5B’SERVER_NAME’%5D%20.%20%24port%3B%0A%24uri%20%3D%20%24protocol%20.%20’%3A%2F%2F’%20.%20%24host%20.%20%0A%24s%5B’REQUEST_URI’%5D%3B%20%24segments%20%3D%20explode(‘%3F’%2C%20%24uri%2C%202)%3B%0A%24url%20%3D%20%24segments%5B0%5D%3B%0A%20return%20%24url%3B%20%7D%20%0A” message=”javascript code” highlight=”” provider=”manual”/]

[pastacode lang=”javascript” manual=”%24url%20%3D%20(isset(%24_SERVER%5B’HTTPS’%5D)%20%26%26%20%24_SERVER%5B’HTTPS’%5D%20!%3D%3D%20’off’%20%3F%20’https’%20%3A%20’http’)%20.%20’%3A%2F%2F’%20.%20%24_SERVER%5B’HTTP_HOST’%5D%20.%20%24_SERVER%5B’REQUEST_URI’%5D%3B%20%0A” message=”javascript code” highlight=”” provider=”manual”/]
  • It works for all webservers (Apache, nginx, IIS, …):

  • we can get full current URL including $_SERVER[‘REQUEST_URI’].

Function:

[pastacode lang=”php” manual=”function%20getCurrentUrl(%24full%20%3D%20true)%20%7B%20%0Aif%20(isset(%24_SERVER%5B’REQUEST_URI%20%7B%0A%0A%24parse%20%3D%20parse_url(%20%0A%20%20%20%20%20%20%20%20(isset(%24_SERVER%5B’HTTPS’%5D)%20%26%26%20strcasecmp(%24_SERVER%5B’HTTPS’%5D%2C%20’off’)%20%3F%20’https%3A%2F%2F’%20%3A%20’http%3A%2F%2F’)%20.%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%09(isset(%24_SERVER%5B’HTTP_HOST’%5D)%20%3F%20%24_SERVER%5B’HTTP_HOST’%5D%20%3A%20(isset(%24_SERVER%5B’SERVER_NAME’%5D)%20%3F%20%20%09%24_SERVER%5B’SERVER_NAME’%5D%20%3A%20”))%20.%20((%24full)%20%3F%20%24_SERVER%5B’REQUEST_URI’%5D%20%3A%20null)%20)%3B%20%0A%20%24parse%5B’port’%5D%20%3D%20%24_SERVER%5B%22SERVER_PORT%22%5D%3B%20%2F%2F%20Setup%20protocol%20for%20sure%20(80%20is%20default)%0A%20return%20http_build_url(”%2C%20%24parse)%3B%0A%20%7D%20%0A%7D%0A” message=”php code” highlight=”” provider=”manual”/] [ad type=”banner”]

Test code:

[pastacode lang=”javascript” manual=”%2F%2F%20Follow%20%24_SERVER%20variables%20was%20set%20only%20for%20test%20%24_SERVER%5B’HTTPS’%5D%20%3D%20’off’%3B%0A%20%2F%2F%20on%20%24_SERVER%5B’SERVER_PORT’%5D%20%3D%20’9999’%3B%20%0A%2F%2F%20Setup%20%24_SERVER%5B’HTTP_HOST’%5D%20%3D%20’some.crazy.server.5.name%3A8088’%3B%0A%20%2F%2F%20Port%20is%20optional%20there%20%0A%24_SERVER%5B’REQUEST_URI’%5D%20%3D%20’%2F150%2Ftail%2Fsingle%2Fnormal%3Fget%3Dparam’%3B%20%0Aecho%20getCurrentUrl()%3B%20%2F%2F%20http%3A%2F%2Fsome.crazy.server.5.name%3A9999%2F150%2Ftail%2Fsingle%2Fnormal%3Fget%3Dparam%20%0Aecho%20getCurrentUrl(false)%3B%20%0A%2F%2F%20http%3A%2F%2Fsome.crazy.server.5.name%3A9999%2F%20%0A” message=”javascript code” highlight=”” provider=”manual”/]

  •  Function to handle the URL:
[pastacode lang=”php” manual=”%3C%3Fphp%20function%20curPageURL()%20%7B%0A%20%24pageURL%20%3D%20’http’%3B%20%0Aif%20(%24_SERVER%5B%22HTTPS%22%5D%20%3D%3D%20%22on%22)%20%7B%24pageURL%20.%3D%20%22s%22%3B%0A%7D%20%24pageURL%20.%3D%20%22%3A%2F%2F%22%3B%0A%20if%20(%24_SERVER%5B%22SERVER_PORT%22%5D%20!%3D%20%2280%22)%20%7B%20%0A%24pageURL%20.%3D%20%24_SERVER%5B%22SERVER_NAME%22%5D.%22%3A%22.%24_SERVER%5B%22SERVER_PORT%22%5D.%24_SERVER%5B%22REQUEST_URI%22%5D%3B%20%0A%7D%20%0Aelse%20%7B%0A%20%24pageURL%20.%3D%20%24_SERVER%5B%22SERVER_NAME%22%5D.%24_SERVER%5B%22REQUEST_URI%22%5D%3B%20%7D%20return%20%24pageURL%3B%0A%20%7D%20%0A%3F%3E%20%0A” message=”php code” highlight=”” provider=”manual”/]

  • HTTPS support:
[pastacode lang=”javascript” manual=”%24current_url%20%3D%20sprint%20%20%0A%20%20%20%20%20(%20’%25s%3A%2F%2F%25s%2F%25s’%2C%20%0A%20%20%20%20%20%20isset(%24_SERVER%5B’HTTPS’%5D)%20%3F%20’https’%20%3A%20’http’%2C%0A%20%20%20%20%20%20%20%24_SERVER%5B’HTTP_HOST’%5D%2C%20%0A%20%20%20%20%20%20%20%20%24_SERVER%5B’REQUEST_URI’%5D%0A%20)%3B%20%0A” message=”javascript code” highlight=”” provider=”manual”/]

Code:

[pastacode lang=”javascript” manual=”%24base%20%3D%20%22http%3A%2F%2F%24_SERVER%5BSERVER_NAME%5D%3A%24_SERVER%5BSERVER_PORT%5D%24my_web_base_path%22%3B%20%0A%24url%20%3D%20%24base%20.%20%22%2F%22%20.%20dirname(dirname(__FILE__))%3B%20%0A” message=”javascript code” highlight=”” provider=”manual”/]

  • Here is the another way of solution:
[pastacode lang=”javascript” manual=”%2F%2FFetch%20page%20URL%20by%20this%0A%20%24url%20%3D%20%24_SERVER%5B’REQUEST_URI’%5D%3B%20%0Aecho%20%22%24url%3Cbr%20%2F%3E%22%3B%20%0A%0A%2F%2FIt%20will%20print%0A%20%2F%2Ffetch%20host%20by%20this%0A%0A%20%24host%3D%24_SERVER%5B’HTTP_HOST’%5D%3B%0A%20echo%20%22%24host%3Cbr%20%2F%3E%22%3B%20%0A%0A%2F%2FYou%20can%20fetch%20the%20full%20URL%20by%20this%20%0A%0A%24fullurl%20%3D%20%22http%3A%2F%2F%22.%24_SERVER%5B’HTTP_HOST’%5D.%24_SERVER%5B’REQUEST_URI’%5D%3B%0A%20echo%20%24fullurl%3B%20%0A” message=”javascript code” highlight=”” provider=”manual”/] [ad type=”banner”]

  • You can use this code:
[pastacode lang=”php” manual=”print_r(%24_SERVER)%3B%20%0A” message=”php code” highlight=”” provider=”manual”/]
  • $_SERVER is an array containing information such as headers, paths, and script locations.
  • The entries in this array are created by the web server.
  • $HTTP_SERVER_VARS contains the same initial information, but is not a superglobal.   (Note that $HTTP_SERVER_VARS and $_SERVER are different variables.)

Categorized in: