1

I was trying to use this code:

if($_SERVER['HTTP_PORT'] !== 443 && !isset($_SERVER['HTTPS'])) {
  header('Location: https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
  exit;
}

to make my login page and register page contain https to use the ssl to make sure that everything is secure, but when I run the page an error message shows up Undefined index error using $_SERVER['HTTPS']

so I decided to use another one:

if ( isset( $_SERVER["HTTPS"] ) && strtolower( $_SERVER["HTTPS"] ) == "on" ) {
  $pageURL .= "s";
}

but it didn't work. The https didn't show up and http is only there...

any idea how to do that with php...

Thanks

The solution is:

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

{ header("Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]); exit(); }

2 Answers 2

2

From the manual:

$_SERVER['HTTPS']
  Set to a non-empty value if the script was queried through the HTTPS protocol.

Therefore, you should be using the following condition to check for an HTTPS connection:

if(!empty($_SERVER['HTTPS']))
{
    // https://...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I tried that, but didn't work. I, however, used another way similar. and here is the code: if($_SERVER["HTTPS"] != "on") { header("Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]); exit(); }
1

You'd better use $_SERVER['SERVER_PORT']. HTTP_PORT doesnt always return the portnumber.

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.