1

Newbie question here, is there any inbuilt PHP tag that can be used to retrieve the URL of a page and echo it on the screen?

Thanks.

Jonathan Holloway
  • 62,090
  • 32
  • 125
  • 150
Jackie
  • 13
  • 2
  • Which page? The current one (i.e., the one being generated by the PHP script itself)? – Matteo Italia Jan 17 '10 at 22:53
  • This is very similar to: http://stackoverflow.com/questions/2079457/www-to-non-www-redirect-with-php/2079463#2079463 . And look, I used the same function to answer it! – Tyler Carter Jan 17 '10 at 22:54

4 Answers4

2

Echos the URL of the current page.

$pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
if ($_SERVER["SERVER_PORT"] != "80")
{
    $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} 
else 
{
    $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
echo $pageURL;
Tyler Carter
  • 60,743
  • 20
  • 130
  • 150
  • +1 very conscise. @OP: You can see all server variables that are available to you (Port, various URIs, request headers and languages, etc.) Using `print_r($_SERVER);` and viewing the output's source code. – Pekka Jan 17 '10 at 22:54
  • It's actually the code that Google uses to find their page url. – Tyler Carter Jan 17 '10 at 22:55
  • Thanks Chacha, this works. I thought there might be some inbuilt tag similar to php_info since this seems like a very standard task but apparently not. – Jackie Jan 17 '10 at 22:55
  • If `$_SERVER["HTTPS"] == "on"`, I believe you can use `$_SERVER["SCRIPT_URI"]` and get the whole thing. Or is that just with Apache? – Anthony Jan 17 '10 at 23:13
  • Someone on this question: http://stackoverflow.com/questions/717874/serverscripturi-not-working-alternative had problems with it. – Tyler Carter Jan 17 '10 at 23:21
1

If your web server runs on standard ports (80 for HTTP or 443 for HTTPS) this would work:

getservbyport($_SERVER['SERVER_PORT'], 'tcp') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
0

Take a look at the $_Server variable. Specifically you probably want the REQUEST_URI value.

Parrots
  • 26,658
  • 14
  • 59
  • 78
0
$base_url = _SERVER["HTTP_HOST"];
$url_path = _SERVER["REQUEST_URI"];

echo $base_url.$url_path;

Assuming the requested page was http://sample.org/test.php, you would get:

 sample.org/test.php

You would have to add more $_SERVER variables to get the scheme (http://). REQUEST_URI also leaves any GET variables intact, so if the page request was http://sample.org/test.php?stuff=junk, you would get:

 sample.org/test.php?stuff=junk

If you wanted that left off, use $_SERVER['PHP_SELF'] instead of REQUEST_URI.

If you want a really easy way to see which global variables are available, create a page with the following:

<?php

phpinfo();

?>

and put that script in any directory you are curious about. Not only will you see all sorts of neat info, you will also see how various factors such as HTTP vs HTTPS, mod_rewrite, and even Apache vs IIS can set some global variables differently or not at all.

Anthony
  • 36,459
  • 25
  • 97
  • 163