I want to display the URL of a page using PHP.

Basically do this:

<script language="javascript">
document.writeln(document.location);
</script>

with PHP

Recommended Answers

All 8 Replies

$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
echo $url

The problem is that I used a form with "get". So I need all that information in the URL. when I use that javascript tag, it displays the correct URL, but it I can't insert the javascript into 'href=' like I can with PHP.

ok then well theres two ways to do this then...

$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']."?".$_SERVER['QUERY_STRING'];
echo $url;

this will always assume there is arguments in the address so u will always see a question mark int the address, a better way of doing this would be to test if there actually was any arguments before just adding the question mark such as in this example:

$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
if (!empty($_SERVER["QUERY_STRING"]))
	$url .= "?".$_SERVER['QUERY_STRING'];

echo $url;

i hope this answers any questions you do have

Thank you! It works wonderfully. Exactly what I wanted. May you live long and prosper.

Godspeed.

:eek:paradox814, you beet me tothe answer again. Good job m8!:p vbmenu_register("postmenu_82884", true);

hey i just rememberd one case in which this won't work!!!

if you are usign a secure ssl http session... in which case replace http:// with https:// (notice the s)

ok man, peace out

No, I'm not. It works fine. Thanks for the heads up though.

how do you do that just with out the file in it... just with the directory in it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.