Finding URL used to access 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
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Finding URL used to access page.

Post 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']);
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

$PHP_SELF returns part of it..
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post by RandomEngy »

But I need all of it. :/
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

take a look at the parse_url() function

http://www.php.net/parse_url
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post 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.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

try

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

when i ran it on my own comp, it came up with "localhost"
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

Code: Select all

if (!$HTTPS)
  header("Location: https://{$_SERVERї'HTTP_HOST']}{$_SERVERї'REQUEST_URI']}");
Works like a charm 8)
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post by RandomEngy »

Alright, thanks protokol and hobgoblin, I got it working!
Post Reply