determining full url to 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
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

determining full url to page

Post by newmember »

let's say my site located at: http://www.webhost.com/~me/main.php

there is a line inside main.php: header("Location: full_url");

I want to specify dynamically complete url in this header...

$_SERVER['script_name'] gives only relative path...
how do i determine the http://..... part ?

thanks
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

$_SERVER["SERVER_NAME"] gives you the server name part.

I don't know off hand how to determine the www part (should be automatic).

Why do you need the header("Location: full_url") when the relative path should send it correctly ?

As an aside the phpinfo() command is great to look up $_SERVER variables etc.
Have a phpinfo page consististing of

Code: Select all

<?php 
  phpinfo();
?>
and take a look.
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

Why do you need the header("Location: full_url") when the relative path should send it correctly ?
i read that specs reqiure full url... so there are maybe UA's that doesn't understand relative path...

i did printed $_SERVER variable so that why i'm asking because i don't see there anything that would hold complete url...
php manual also says that:
If the script is running on a virtual host, this will be the value defined for that virtual host.
so i'm not sure whether it's good to use $_SERVER['SERVER_NAME']

in my case $_SERVER['SERVER_NAME'] contains ip and not dns name... is it always the case?


so i'm not sure if i can use this variable because of "virtual host" thing...
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

i just tried to give a relative url:
header('Location: /en/mainen.php');

and i got:

The requested URL /en/mainen.php was not found on this server.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$_SERVER['HTTP_HOST']

if $_SERVER['HTTPS'] is set, then you are running under https, otherwise, it's an http request.
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

ok thanks
i already solved this:
header('Location: http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/en/mainen.php');
i don't know if it is flexable enough (maybe i'll have to change it when i'll put it on real webhost) but for now it works...
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

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;
}
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Wow, nice function. :mrgreen:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

needs to swap a few lines though ;)
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

thanks for that piece of art:)
Post Reply