redirect if https is set

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Craig Keightley

    redirect if https is set

    i have the following script on my checkout page to check if https is set in
    the address bar:

    if ($_SERVER['HTTPS'] != "on")
    {
    $url = $_SERVER['SERVER_NAME'];
    $query = $_SERVER['QUERY_STRING'];
    $path = $_SERVER['PHP_SELF'];
    header("Locatio n: https://$url$path?$quer y");
    }


    my problem is that i need to escape from https when another page is
    selected. I tried changing the code on other pages (an include file) to
    read:

    if ($_SERVER['HTTPS'] != "off")
    {
    $url = $_SERVER['SERVER_NAME'];
    $query = $_SERVER['QUERY_STRING'];
    $path = $_SERVER['PHP_SELF'];
    header("Locatio n: http://$url$path?$quer y");
    }


    but to no avail. Is this right?

    Craig


  • Tim Roberts

    #2
    Re: redirect if https is set

    "Craig Keightley" <dont@spam.me > wrote:
    [color=blue]
    >i have the following script on my checkout page to check if https is set in
    >the address bar:
    >
    >if ($_SERVER['HTTPS'] != "on")
    >{
    > $url = $_SERVER['SERVER_NAME'];
    > $query = $_SERVER['QUERY_STRING'];
    > $path = $_SERVER['PHP_SELF'];
    > header("Locatio n: https://$url$path?$quer y");
    >}
    >
    >
    >my problem is that i need to escape from https when another page is
    >selected. I tried changing the code on other pages (an include file) to
    >read:
    >
    >if ($_SERVER['HTTPS'] != "off")
    >{
    > $url = $_SERVER['SERVER_NAME'];
    > $query = $_SERVER['QUERY_STRING'];
    > $path = $_SERVER['PHP_SELF'];
    > header("Locatio n: http://$url$path?$quer y");
    >}
    >
    >but to no avail. Is this right?[/color]

    If the request is not https, the HTTPS variable will simply not exist. The
    variable will NEVER equal "off".

    Just use this:

    if( $_SERVER['HTTPS'] == "on" )

    Or, probably better:

    function IsLinkSecure()
    {
    return isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == "on");
    }

    if( IsLinkSecure() )
    {
    ...
    }
    --
    - Tim Roberts, timr@probo.com
    Providenza & Boekelheide, Inc.

    Comment

    • R. Rajesh Jeba Anbiah

      #3
      Re: redirect if https is set

      Craig Keightley wrote:[color=blue]
      > i have the following script on my checkout page to check if https is[/color]
      set in[color=blue]
      > the address bar:
      >
      > if ($_SERVER['HTTPS'] != "on")[/color]

      <snip>

      if (!empty($_SERVE R['HTTPS']))
      echo 'secure';

      --
      <?php echo 'Just another PHP saint'; ?>
      Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

      Comment

      Working...