Force A Secure Page Using PHP

By  on  

Many pages, most often pages with forms or pages that serve personal information, require the need to be served over a secure connection. Even recreational internet users have gotten accustomed to looking for "lock" icon within their browser before inputting data into a web form. For the benefit of the business and its website visitors, it's important to ensure that a form page be secured.

To ensure that you page is served over a secure connection, you must first acquire a security certificate. Popular SSL certificate providers include Verisign, Thawte, and GoDaddy (whom I prefer). Once your SSL certificate has been installed on the server, you may add the following code snipped at the top of any page you would like secured:

The PHP Code

//force redirect to secure page
if($_SERVER['SERVER_PORT'] != '443') { header('Location: https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); exit(); }

The above code forces the script to run on secure port 443 as opposed to port 80. Thus, the page is served securely.

Recent Features

  • By
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

Incredible Demos

  • By
    Create a Trailing Mouse Cursor Effect Using MooTools

    Remember the old days of DHTML and effects that were an achievement to create but had absolutely no value? Well, a trailing mouse cursor script is sorta like that. And I'm sorta the type of guy that creates effects just because I can.

  • By
    PHP / MooTools 1.2 Accordion Helper

    The MooTools Accordion plugin seems to be the plugin that people seem to have the most problems with. It's an awesome plugin, so I can see why so many people want to use it, but I think that may be part of the problem.


  1. niaomi

    This doesn’t work :-(

    Do the webpage need to be php? The page I need secure is html.

    Please help. Thanks!

  2. april

    Works just fine for me. Thanks!

  3. pete

    niaomi, the code is php so yeah it will need to be run on a php page!!

  4. @niaomi: yes niaomi, it must be PHP

  5. Mikey

    Didn’t work for me. Got error message “Warning: Cannot modify header information – headers already sent”.

    Probably has something to do with my hosting company….who knows.

    • Check your session_start(); I had an issue like his before, I had to silence it with ob_start();

  6. alex

    but how to get back to normal http page from https?

  7. rohan bagchi

    @Mikey on June 1, 2011 @ 10:53 am
    Didn’t work for me. Got error message “Warning: Cannot modify header information – headers already sent”.
    Probably has something to do with my hosting company….who knows.
    you cannot use header in a page that has any output before it.
    html is an you use header after html data,and you get the error you did.

  8. Brandon

    Didn’t work for me either. Am I supposed to add the tags around the code you shows above? I am also getting the cannot modify header information error and I don’t understand your reply above.

  9. Brandon

    That should have said the php tags around the code you show above.

  10. brandon,it will work just fine.
    do this.
    opn d php file and at the beginning of the file before u start any code,do this:

  11. Worked beautifully for me right out of the box. Those having problems, here is what you need to do.

    Place this at the tippety-top of your PHP web page:
    Once you’ve done that, and saved it as “whatever.php”, it should work like a charm.

  12. Hi,

    its working great for me but as Alex asked, how do you get back to non ssl when you navigate away from that page?… it seems to make everything after SSL protected.

    Thanks in advance!

  13. Using it the opposite way:

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

    and yes, wrap in PHP tags: on a PHP page.

  14. Ross

    This works as described except that when I navigate back to other pages on the site, they are loading as https:// which I would prefer not happen… can you offer an adjustment to correct this? Many thanks, Ross.

  15. Mark

    Another way could be:

    if(empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on") {
    	header('Location: https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); exit();
  16. James

    This might not work in all cases though. I use CloudFlare SSL which is SSL between the browser and the CDN. Checking the server settings for HTTPS won’t actually work as they’re not set for SSL.


      // replace exit();
      // with die();  //  why you might ask: it so happens that exit(); causes browsers to see your url as insecure

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!