Page 1 of 1

Finding URL used to access page.

Posted: Wed Jul 03, 2002 2:28 pm
by RandomEngy
I know It seems like a simple question, but I couldn't find the answer in my searches. What I want is to get the URL the person typed in their browser to get to the page.

The reason I need this, is that I'm using SLL on my server, and I want users who type in "http://www.company.com/login" to be redirected to "https://www.company.com/login" . I know how to tell if https was used or not, but I don't know how to get the http://www.company.com portion. I was thinking:

Code: Select all

if(!$HTTPS)
  header("Location: https://".$base_path_or_something.$_SERVERї'PHP_SELF']);

Posted: Wed Jul 03, 2002 2:34 pm
by hob_goblin
$PHP_SELF returns part of it..

Posted: Wed Jul 03, 2002 2:45 pm
by RandomEngy
But I need all of it. :/

Posted: Wed Jul 03, 2002 2:50 pm
by hob_goblin

Posted: Wed Jul 03, 2002 2:53 pm
by hob_goblin
and, if you have more than you need you could just use explode() and remove some of the entries you don't need

Posted: Wed Jul 03, 2002 2:59 pm
by protokol
take a look at the parse_url() function

http://www.php.net/parse_url

Posted: Wed Jul 03, 2002 3:08 pm
by RandomEngy
Hmmm, I couldn't find anything in those links that would tell me the part of the URL I needed. I just need to get that info, I'm fairly sure of what to do with it afterwards.

Posted: Wed Jul 03, 2002 3:13 pm
by hob_goblin
try

$url = getenv('SERVER_NAME');
echo $url;

when i ran it on my own comp, it came up with "localhost"

Posted: Wed Jul 03, 2002 3:18 pm
by protokol

Code: Select all

if (!$HTTPS)
  header("Location: https://{$_SERVERї'HTTP_HOST']}{$_SERVERї'REQUEST_URI']}");
Works like a charm 8)

Posted: Wed Jul 03, 2002 3:55 pm
by RandomEngy
Alright, thanks protokol and hobgoblin, I got it working!