Force A Secure Page Using PHP
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.
![5 More HTML5 APIs You Didn’t Know Existed]()
The HTML5 revolution has provided us some awesome JavaScript and HTML APIs. Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers. Regardless of API strength or purpose, anything to help us better do our job is a...
![CSS @supports]()
Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS. What we end up doing is repeating the same properties multiple times with each browser prefix. Yuck. Another thing we...
![Style Textarea Resizers]()
Modern browsers are nice in that they allow you to style some odd properties. Heck, one of the most popular posts on this blog is HTML5 Placeholder Styling with CSS, a tiny but useful task. Did you know you can also restyle the textarea resizer in WebKit...
![MooTools Window Object Dumping]()
Ever want to see all of the information stored within the window property of your browser? Here's your chance.
The XHTML
We need a wrapper DIV that we'll consider a console.
The CSS
I like making this look like a command-line console.
The MooTools JavaScript
Depending on what you have loaded...
This doesn’t work :-(
Do the webpage need to be php? The page I need secure is html.
Please help. Thanks!
Works just fine for me. Thanks!
niaomi, the code is php so yeah it will need to be run on a php page!!
@niaomi: yes niaomi, it must be PHP
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 withob_start();
but how to get back to normal http page from https?
@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 output.so you use header after html data,and you get the error you did.
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.
That should have said the php tags around the code you show above.
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:
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.
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!
Using it the opposite way:
and yes, wrap in PHP tags: on a PHP page.
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.
Another way could be:
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.
Correct?