Redirect browser to https (ssl) using php

June 11th, 2012 by laeeq | 1 comment

As we know that all transactions over internet uses SSL (secure socket layer) connection to transfer data to and from the payment gateway. commonly most of websites uses “HTTP” protocol but When we need to integrate payment gateways on e-commerce site or any other. Then we need to redirect the browser from common “HTTP” to Secure “HTTPS” which mean that “Hypertext Transfer Protocol over Secure Socket Layer”.

We can see the real example of HTTPS in Banks, reservation sites when we type “HTTP” it automatically converts to “HTTPS” in address bar which means this site is transferring the data over SSL protocal.

Redirect the browser to https when site is using http protocal in PHP

First of all, you will have to configure SSL on your server. After we will check that the site is using SSL or not by PHP server varriable called $_SERVER['HTTPS'] which return “ON” valuewhen the site is using SSL Connection.

PHP Function to redirect the browser to “https”
  1. function redirectToSecureHTTPS()
  2. {
  3. if($_SERVER['HTTPS']!=“on”)
  4. {
  5. $redirect= “https://”.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  6. header(“Location:$redirect”);
  7. }
  8. }

This is a very simple php fucntion, you can call this function in the page where you’ve to redirect the browser to “https”.

Redirect the browser to “https” using .htaccess

The above php function need to be call on each page to redirect the browser to “https”. But by using .htaccess file, you can redirect the whole website to use SSL connection throughout the pages.

  1. RewriteEngine On
  2. RewriteCond %{HTTPS} !on
  3. RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

You just need to copy and paste the above code in .htaccess file and the whole website will be redirected to “https”. The browser will be et redirected using url rewriting in .htaccess.

 

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

  • Wrapping Text using PHP
  • PHP 5.5 beta 4 is now available
  • Swapping Array Keys and Values
  • PHP Security
  • Working with Image Metadata in PHP
  • Converting Between Relative and Absolute File Path
  • Parsing File Paths with PHP
  • How to remove HTML Comments with PHP?
  • How to access .htaccess values?
  • Dealing With Common PHP Errors
  • Interview Questions and Answers for Freshers
  • Display numbers with ordinal suffix using PHP
  • Function to set COLLATION on database fields of Mysql
  • Read and write to remote files using PHP
  • Sharing PHP SESSION Variables between Multiple Subdomains
  • PHP 5.5.0 Alpha4 Development Preview Released
  • Check a string starts with http using PHP
  • Cross-Site Scripting Attacks (XSS)
  • PHP 5.4.11 and PHP 5.3.21 released!
  • PHP Memory Leak Issue
  •  

     

    1. October 7th, 2012 at 12:03 | #1

      What a stuff of un-ambiguity and preserveness of valuable know-how concerning unexpected
      feelings.

    1. No trackbacks yet.