Page 1 of 1

Capture URL of current page

Posted: Thu Mar 25, 2004 10:25 pm
by pbritten
If it's possible, would someone be so kind as to tell me how to capture the URL of the current page and save it in a variable?

Thank you very much!

Pat

Posted: Thu Mar 25, 2004 10:33 pm
by Goowe

Code: Select all

<?php
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

echo $url;
?>
I think this works every time... might not though... don't shoot me if it doesn't :roll:

Posted: Fri Mar 26, 2004 1:28 am
by reverend_ink
Might try

Code: Select all

<?php 
$url = getenv( "HTTP_REFERER" );
echo $url;

?>

Posted: Fri Mar 26, 2004 2:14 am
by m3mn0n
reverend_ink wrote:Might try

Code: Select all

<?php 
$url = getenv( "HTTP_REFERER" );
echo $url;

?>
REFERER is the page that sent a user to the current page. And this is one not recommended to work with because it can be easily spoofed.

Posted: Fri Mar 26, 2004 2:58 am
by reverend_ink
You know what you're right... I have been working on REFERERs all week.

Posted: Fri Mar 26, 2004 8:55 am
by pbritten
Goowe wrote:

Code: Select all

<?php
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

echo $url;
?>
I think this works every time... might not though... don't shoot me if it doesn't :roll:
I tried this, but only was able to capture the domain name, not the folder and filename. Any suggestions?

Posted: Fri Mar 26, 2004 9:14 am
by pbritten
This seems to work. Thank you for helping me find where to start looking.

<?php

$completePath = $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

echo "$completePath";
?>

Posted: Fri Mar 26, 2004 11:30 am
by pickle
try

Code: Select all

$complete_path = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

Posted: Fri Mar 26, 2004 11:33 am
by aleigh
HTTP_HOST is not always present, sadly, but this works well enough. That variable is slaved to the HTTP "Host:" field which very old browsers and strange clients (Like OSX's "ftp"'s http support) don't always implement.

PHP does not have a clear concept of the IP that the people contacted to start the request. On my server I have been tempted to pass it into PHP into a new variable but haven't done any work on this.

You could also get the local endpoint of the socket your request is serving on but this is definitly not worth your time.

Posted: Fri Mar 26, 2004 11:35 am
by vigge89
or:

Code: Select all

<?php

$complete_path = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].$_SERVER['REQUEST_URI'];
?>