PHP get current page URL


Home - Tutorials - Navigation

It can happen that you want to know the current URL - the URL in the browser window - in your PHP application. A good example if you want to make some special action depending on the actual URL.

Tutorial info:


Name:PHP get current page URL
Total steps:1
Category:Navigation
Level:Beginner

Bookmark PHP get current page URL



Bookmark and Share

Step 1 - Get current page URL


PHP get current page URL

In PHP there are some built in global variables that make the getting the current URL process quite simple. You can get every piece of information about the current URL using the $_SERVER suberglobal array.
You can get the domain name, the script name, the url parameters and any variation of these.
After this introduction let’s see how it works. First of all try to figure out a complex URL structure like this:

http://www.demo.com/test/myscript.php?city=Dallas&start=10

Let’s take this URL to pieces:

And now lets’s try to build the same URL with PHP. 

The url protocol can be read out from the $_SERVER[‘SERVER_PROTOCOL’] variable. 

Code:
  1. echo $_SERVER['SERVER_PROTOCOL'];

However if you check the value you can find that is not simply http or https but a string like this: HTTP/1.1

So we need some string manipulation to get a clean protocol string:

Code:
  1. $protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') 
  2.                 === FALSE ? 'http' : 'https';

As next step we need to figure out the hostname. This is in the $_SERVER[‘HTTP_HOST’]. You can read it simply like this:

Code:
  1. $host = $_SERVER['HTTP_HOST'];

The $_SERVER[‘SCRIPT_NAME’] contains the full path with the name of the actual php script as you can see here:

Code:
  1. $script = $_SERVER['SCRIPT_NAME'];

The last part of the current url is in the $_SERVER[‘QUERY_STRING’] and can be accessed similar to the others:

Code:
  1. $params = $_SERVER['QUERY_STRING'];

If the protocol and host name is not important for you but only the path, script name and the URL parameters then you can use simply $_SERVER[‘REQUEST_URI’] the as follows:

Code:
  1. $uri = $_SERVER['REQUEST_URI'];

 

So get the current page URL in PHP looks like this:

Code:
  1. $protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') 
  2.                 === FALSE ? 'http' : 'https';
  3. $host     = $_SERVER['HTTP_HOST'];
  4. $script   = $_SERVER['SCRIPT_NAME'];
  5. $params   = $_SERVER['QUERY_STRING'];
  6.  
  7. $currentUrl = $protocol . '://' . $host . $script . '?' . $params;
  8.  
  9. echo $currentUrl;

 

Note: Almost all modern webserver with PHP supports the used $_SERVER variables, you need to know that: “The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these.”  See PHP $_SERVER manual

 







Tags: php get current url, get page url, php get url, get actual url, get query string, get hostname, php

PHP get current page URL - Table of contents
Step 1 - Get current page URL


Follow phpf1 on Twitter




F1 Site Family
AJAX F1
CSS F1
Database F1
Flash F1
HTML F1
Java F1
JavaScript F1
PhotoShop F1
PHP F1
Scripts F1
Tutorial F1
Windows F1

Total time: 0.0346