2

I'm trying to roll out HTTPS on a small website with minimal changes, I am adding code at the top of the pages I want protected...

<?php
$securepage ="1";

if ($_SERVER['HTTPS']=='on') {
    // we are on a secure page.
    if (!$securepage) {
      // but we shouldn't be!
      $url='http://www.mywebsite.com'.$_SERVER['REQUEST_URI'];
      header('location: '.$url);
      exit;
    }
  } else {
    // we aren't on a secure page.
    if ($securepage) {
      // but we should be!
      $url='https://www.mywebsite.com'.$_SERVER['REQUEST_URI'];
      header('location: '.$url);
      exit;
    }
  }
?>

The pages I don't want protected I remove the following...

$securepage ="1";

but when I try loading the page I get an error saying the page was redirected too many times.

Anyone have any pointers as to what is wrong with the code above?

1

1 Answer 1

1

I'm in the process of developing an app, with the same requirement. Some pages needs to be viewed over SSL (https) and others do not.

For pages that need to be viewed over SSL, I use

if ($_SERVER["SERVER_PORT"]==80){ header("Location: https://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']); exit(); }

For those that do not, i.e. can be viewed over http, use

if ($_SERVER["SERVER_PORT"]!=80){ header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']); exit(); }

if your page is within a directory on your host, then this may help too

"location:http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])";

Hope this helps...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.