Capture URL of current page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pbritten
Forum Newbie
Posts: 13
Joined: Wed Mar 24, 2004 9:24 am
Location: State College, Pennsylvania, USA

Capture URL of current page

Post 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
Goowe
Forum Commoner
Posts: 94
Joined: Mon Mar 15, 2004 9:51 am
Location: Southeast Alaska

Post 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:
reverend_ink
Forum Contributor
Posts: 151
Joined: Sun Apr 20, 2003 1:18 am
Location: Las Vegas | London

Post by reverend_ink »

Might try

Code: Select all

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

?>
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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.
reverend_ink
Forum Contributor
Posts: 151
Joined: Sun Apr 20, 2003 1:18 am
Location: Las Vegas | London

Post by reverend_ink »

You know what you're right... I have been working on REFERERs all week.
pbritten
Forum Newbie
Posts: 13
Joined: Wed Mar 24, 2004 9:24 am
Location: State College, Pennsylvania, USA

Post 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?
pbritten
Forum Newbie
Posts: 13
Joined: Wed Mar 24, 2004 9:24 am
Location: State College, Pennsylvania, USA

Post 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";
?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

try

Code: Select all

$complete_path = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
aleigh
Forum Commoner
Posts: 26
Joined: Thu Mar 25, 2004 11:06 am
Location: Midwestern United States
Contact:

Post 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.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

or:

Code: Select all

<?php

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