Hello,

I want to use the secure protocol https if available, so anyone requesting http://mydomain.net/login.php is redirected to https://mydomain.net/login.php

if (getenv("HTTPS") == 'on')
{
header ("Location: [url]https://[/url]".$SERVER['HTTP_HOST'].$SERVER['PHP_SELF']);
exit ();
}

In MSIE I get an error stating problems with redirections, while Opera seems to be endlessly refreshing the page.

Question 1: What's wrong with my script?

Q2: Is there a way to know whether the browser requested a URL starting with https or not?

Thanks in advance!

    There is actually a way of changing http to https by using a .htaccess file.
    It looked better to me then trying to get a php script to detect all that, but I wasn't able to get the .htaccess method to work for me (wasn't able to spend much time on it).

    You may also want to put SERVER variables into other variables. I found that $foo['bar'] will make header() not work correctly. So maybe something like $path1=$SERVER['HTTP_HOST'].

    Anyway, there is my 2 cents. Sorry I couldn't help more.

    Good luck!

      In one of the other threads I found this script:

      if($HTTP_SERVER_VARS["HTTPS"] != "on")
      {
      $newurl = "https://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
      header("location: $newurl");
      }
      

      It works when SSL is present on the server, but I'm not sure
      what will happen if it isn't. Possibility might be to put this if-test
      inside another if-test veryfying whether SSL is available.

      Alfa

        Write a Reply...