PHP Script Switch Page to HTTPS

Here is a quick php script that you can add to the top of any page you want to be secure. Obviously, it requires you actually have an SSL Certificate all set up on your domain first.

<?php
if(empty($_SERVER['HTTPS']))
{
    // If not, redirect
    $newurl = 'https://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
    header("location: $newurl");
    exit();
}
 
?>

1 Comments
  1. 411 New York July 24, 20115:06 pm

    Found this page on Google by typing “switch to htpps script.”

    We were actually looking for a script that created a link on the page that it is on.
    It works great when tested. Our only problem is that the page has Adsense
    so there is a warning. If we hack the Adsense code, we risk Google dropping
    us from their Adsense.

Submit Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

To include your code, use pastebin, and paste the URL in the comment body. Failure to do so may prevent your comment from appearing. Thank you.