getting url of page you are on

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

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

getting url of page you are on

Post by Luke »

What is the best way to get the url of the current page? I need to do a Validate this page's XHTML link and for some reason I can't remember how to get the current page's url... :?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

thanks :oops:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There's a user function that's been posted several times.. dig through search.php?search_keywords=geturl
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Code: Select all

$full_url = 'http://' . $_SERVER["SERVER_NAME"] . $_SERVER["SCRIPT_NAME"] . '?' . $_SERVER["QUERY_STRING"];

// or

$full_url = 'http://' . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
You can also sort out the protocol scheme from $_SERVER["SERVER_PORT"].
(#10850)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

wait a minute... wouldn't it be the uri? uri is the address and url is the actual box you type it into right?

Funny how noobish I am at somethings. :lol:
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

viewtopic.php?t=30090

(thanks feyd)
SteveB
Forum Newbie
Posts: 7
Joined: Wed Aug 23, 2006 10:16 am

Post by SteveB »

The Ninja Space Goat wrote:wait a minute... wouldn't it be the uri? uri is the address and url is the actual box you type it into right?

Funny how noobish I am at somethings. :lol:
Direct from Wikipedia:
A Uniform Resource Locator (URL) is a Uniform Resource Identifier which, “in addition to identifying a resource, [provides] a means of locating the resource by describing its primary access mechanism (e.g., its network ‘location’).”
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

by describing its primary access mechanism
the protocol no?

Access mechanism and Network location are not the same, why is one being used as an example of the other?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

timvw wrote:this is the function i found here (or somewhere else)

Code: Select all

function geturl()
{
  $ports = array('https' => 443, 'http' => 80);
  $prefix = empty($_SERVER['HTTPS']) ? 'http' : 'https';
  $url = $prefix;
  $url .= $_SERVER['SERVER_PORT'] != $ports[$prefix] ? ':' . $_SERVER['SERVER_PORT'] : '';
  $url .= '://';
  $url .= $_SERVER['HTTP_HOST'];
  $url .= $_SERVER['REQUEST_URI'];
  return $url;
}

echo geturl();
Won't that make urls like this:

Code: Select all

http:80://www.yahoo.com
I just tried that in the browser address bar. It doesn't work
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Better solution:

The validation service can check the referer... just use this:
http://validator.w3.org/check?uri=referer
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

The CSS validator does the same thing. The only catch is it needs to be hosted on a resolvable domain (so localhost will not work).
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

CSS Validator referer link doesnt' work for me. I think it has something to do with my mod_rewrite rules or something... ? :?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I'd be willing to bet you are not using full URI's in your CSS path, if the validator does not validate.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

:oops:
Post Reply