How to Find the Current URL with PHP
Published 2 years, 9 months ago in Basics and PHPThe full URL to a page comes in three parts: The domain name, the path to the file then the filename, and the query string. For example, take the URL http://www.example.com/example/page.php?name=Bob. The three parts of this are:
1. The domain name: www.example.com
2. The path to the page: /example/page.php
3. The query string: name=Bob
So how do you find it all out with your own PHP scripts?
All of the information we need is stored in the $_SERVER array, which is accessible from anywhere in your PHP script (and as such is called a superglobal variable), it works like a normal array and the keys we wish to retrieve the values of are 'HTTP_HOST', 'SCRIPT_NAME' and 'QUERY_STRING' for the three different parts of the url. Alternatively, if we don't need to have the path to the page and the query string seperate, we can use 'REQUEST_URI'.
The following code should let you find it:
-
<?php
-
// find out the domain:
-
$domain = $_SERVER['HTTP_HOST'];
-
// find out the path to the current file:
-
$path = $_SERVER['SCRIPT_NAME'];
-
// find out the QueryString:
-
$queryString = $_SERVER['QUERY_STRING'];
-
// put it all together:
-
$url = "http://" . $domain . $path . "?" . $queryString;
-
echo "The current URL is: " . $url . "<br />";
-
-
// An alternative way is to use REQUEST_URI instead of both
-
// SCRIPT_NAME and QUERY_STRING, if you don't need them seperate:
-
$url2 = "http://" . $domain . $_SERVER['REQUEST_URI'];
-
echo "The alternative way: " . $url2;
-
?>
If you have a low-traffic website, consider joining the Money4Banners advertising network. They will pay you £10 + £5 each month for displaying a small advert on three of your pages, regardless of traffic. American webmasters are welcome, and since £10 == $20, you make more!
5 Responses to “How to Find the Current URL with PHP”
Leave a Reply
You must log in to post a comment.
Search
Categories
- ASP (3)
- ASP Basics (3)
- Blogs (2)
- C# (1)
- deals (1)
- Firefox (1)
- Flash (1)
- Google (6)
- Hacks (4)
- Java (1)
- PHP (12)
- Basics (8)
- Files (1)
- Image Handling (3)
- Puzzles (1)
- Ruby (1)
- Tips (10)
- Web Hosting (3)
- Web Hosting Articles (1)
- Web Hosting News (2)
- Windows (6)
- Wordpress (2)
Related Entries
- How to Find the Current URL in ASP
- PHP
- Hacking the translation plugin for nicer URLs
- How to Access a Page in Google's Cache
- Generate a Month Calendar In PHP
- Find a visitor's IP Address with PHP
- How to use the query string in PHP
- Finding Items in an Array with PHP
- How to use PHP in pages with a .html extension (or any other)
- Generating Random Numbers in PHP
You might want to get the https as well.
$http = ($_SERVER[’HTTPS’] ? ‘https://’ : ‘http://’);
$url2 = $http . $domain . $_SERVER[’REQUEST_URI’];
How about anchor link? #anchor
thanks dear,
very very thanking you.
These techniques dont work for me, wont solve my problem. These methods will tell you what the file path is of the script that is running it. It is also possible to have one script initiate another with the include() command… which still works according to your techniques. The URL that is returned will be that of the highest level, the first script that initiated the cascade of scripts.
But, HTML is capable of referencing PHP, say, in an , , or tag or something (like in the case of GD Image Libraries). The above techniques will not work for this situation. The above techniques will only return the URL of the highest level PHP script file, not the URL in the address bar, the URL of the page being displayed, the page that initiated the script in the first place.
Is there a way to read directly the URL that the browser is displaying? This would be the highest level URL in any given window.
I have been looking for this for quite some time, thanks