Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Force A Secure Page Using PHP

8 Responses »

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.

Discussion

  1. niaomi
    November 30, 2008 @ 5:00 pm

    This doesn’t work :-(

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

    Please help. Thanks!

  2. april
    April 10, 2009 @ 4:22 pm

    Works just fine for me. Thanks!

  3. pete
    October 19, 2009 @ 3:15 pm

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

  4. February 15, 2010 @ 2:07 pm

    @niaomi: yes niaomi, it must be PHP

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!