Jump to content

[SOLVED] Geting the current url


SyncViews

Recommended Posts

How can I get the url for the current page in php?

 

I tried these but none returned the exact url :(

 

 

$_SERVER['SCRIPT_NAME']

$_SERVER['PHP_SELF']

__FILE__

 

I just want it to to just get the same url as dsiplayed in the user adress bar.. (includeing the domain and ?myvar = value etc). I'd also rather this can work from within an included file and still returnt he correct url)

 

Link to comment
https://forums.phpfreaks.com/topic/82032-solved-geting-the-current-url/
Share on other sites

You can use $_SERVER['REQUEST_URI'] for that.

 

Edit: That won't get the entire URL though. You'll need to combine some different variables like this:

<?php
switch($_SERVER['SERVER_PORT'])
{
default:
case 80:
	$url = 'http';
	break;
case 443:
	$url = 'https';
	break;
}
$url .= '://'.$_SERVER['SERVER_NAME'].(!in_array($_SERVER['SERVER_PORT'], array(80, 443)) ? ':'.$_SERVER['SERVER_PORT'] : null).$_SERVER['REQUEST_URI'];

echo $url;
?>

that didn't return the entire url though. just the bit that comes after the domain :(

(eg"/forums/index.php/topic,172438.0.html")

 

I want a way to get the domain as well so when I move the site onto the internet I don't have to go through and change stuff like that...

 

(so I want "http://www.phpfreaks.com/forums/index.php/topic,172438.0.html" rather than the above)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.