Get Full URL Inc "http://"

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
gbow
Forum Newbie
Posts: 12
Joined: Wed Feb 18, 2004 6:28 am

Get Full URL Inc "http://"

Post by gbow »

Hi

I need to check whether my user has visited http:// or https:// on my site. I have been trying to gather the url string from the location bar but can only seem to get the parts after the http://. The reason I need this is so that I can redirect to https:// if they have accidentally visited through unsecure http://.

We have the SSL certificate installed but I would appreciate any ideas on how i can get this redirect going.

Cheers

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

Post by m3mn0n »

I was wondering about this too. Here is something I grabbed from a Google search:

Code: Select all

<?php
//Determine protocol of web pages 
if( isset ($_SERVER['HTTPS']) && strcasecmp ($_SERVER['HTTPS'],'ON') == 0) 
{
    $conf['protocol'] = 'https://';
} else {
    $conf['protocol'] = 'http://';
} 
?>
gbow
Forum Newbie
Posts: 12
Joined: Wed Feb 18, 2004 6:28 am

Post by gbow »

thats cool but could you give me a quick talk through it?
I have just tried using the following - see what you think

-----

// ***GET URL & PUT https:// IN FRONT OF IT TO SET THE URL FOR REDIRECT***

$secure_url = sprintf("%s%s%","https://",$HTTP_HOST,$REQUEST_URI);

// ***CHECK SERVER PORT***

if($SERVER_PORT == 80) {

// ***REDIRECT TO SECURE VERSION OF THE CURRENT URL
Header("Location: $secure_url");

}

-----

$SERVER_PORT gives either 80 or 443 i have found
if its 80 then http:// was typed in
443 = https://

if u can see any glaring problems with this then please let me know!
im not sure about using $SERVER_PORT cos it seems to be using superglobals which im not happy about but i cudnt find another way to get the information. maybe ur way will help with this if u cud explain it to me.

Cheers
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

There is HTTPS element set in $_SERVER superglobal on https requests:

Code: Select all

if(isset($_SERVER["HTTPS"])){
  //redirect to https
}else{
  //redirect to http
}
$SERVER_PORT is not an option because you can run Apache at any port you want, even plain http at port 443 ;). Use $_SERVER["HTTPS"] . Or just $HTTPS if you don't like superglobals and have your register_globals turned on (for some weird reason ;) )
Brian
Forum Contributor
Posts: 116
Joined: Thu Apr 18, 2002 5:33 pm

A Simple Solution

Post by Brian »

This is not a problem that you should be solving via PHP. You should not have your secure and non-secure pages mapped to the same Web space.
gbow
Forum Newbie
Posts: 12
Joined: Wed Feb 18, 2004 6:28 am

Post by gbow »

Thanks for your help guys
I used

if(isset($_SERVER["HTTPS"])){
// do nothing
}else{
// redirect to https
}

this seems to work well - many thanks
the port thing would have worked for us because we control the ports in this case but we wanted a solution we could use for all sites so your idea is better for future use. :D

is there a list anywhere (that you know of) of all the globals similar to the one i used here? because it would be useful to know what superglobals there are. might save me time in future trying to figure stuff like this out. have a nice day.

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

Post by m3mn0n »

You can see them all by making a simple script that contains:

Code: Select all

<?php
phpinfo();
?>
And that way you can see all the variables.

Or a more complex way:

Code: Select all

<?php
foreach ($_SERVER as $key => $value)
{
   echo "[". $key. "] is set to [". $value ."]\n";
}
?>
This will just show the $_SERVER variables instead of all the rest of that stuff.
Morpheus
Forum Newbie
Posts: 6
Joined: Thu Mar 11, 2004 7:00 am
Location: South Africa

Post by Morpheus »

What about:

Code: Select all

<?php
if (strpos ($PHP_SELF, "https://") !== false) {
   // in https:// site
} else {
   // in http:// site
} // end if
?>
You have to use "!== false", because if it finds "https://" it'll probably by at position zero (which amounts to false if you parse it).

Njoy ;)
Post Reply