PHP
  Home arrow PHP arrow Page 4 - Managing Secure Protocol in Apache-Based Websites using PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Managing Secure Protocol in Apache-Based Websites using PHP
By: Codex-M
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2009-05-28


    Table of Contents:
  • Managing Secure Protocol in Apache-Based Websites using PHP
  • First Potential Problem: Duplicate Content!
  • Second Duplicate Content Solution
  • Second Potential Problem: 301 Redirection from non-secure to secure protocol

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Managing Secure Protocol in Apache-Based Websites using PHP - Second Potential Problem: 301 Redirection from non-secure to secure protocol
    ( Page 4 of 4 )

    There are times when you do really need to redirect the http to the https version. A good example would be to convert an http URL to the https version of the URL so that any transaction between the browser and the server that happens on that URL will be encrypted.

    Another good example would be submitting sensitive data such as passwords, security numbers or even credit card information.

    So how do you go about converting the non-secure protocol to a secure protocol? Using PHP you can force a specific URL to 301 redirect to its equivalent https version.

    See an example below:


    <?php

    function url() {

    $urlofthepage = 'http';

    if ($_SERVER["HTTPS"] == "on") {$urlofthepage .= "s";}

    $urlofthepage .= "://";

    if ($_SERVER["SERVER_PORT"] != "80") {

    $urlofthepage .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];

    } else {

    $urlofthepage .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

    }

    return $urlofthepage;

    }

    $urlofthepage=url();

    if ($urlofthepage=="http://www.thisistheurlthatyouneedtoredirecttohttpsversion.com/
    securecontactexampleform.php")

    {

    // Permanent redirection

    header("HTTP/1.1 301 Moved Permanently");

    header("Location: https:// www.thisistheurlthatyouneedtoredirecttohttpsversion.com/securecontactexampleform.php ");

    exit();

    }

    ?>


    Place this code on top of any HTML code, or before it outputs any HTML. This script must be placed on top of all PHP or HTML code. If it isn't, it won't redirect at all, and in fact will return an error.

    Note that the above sample script only redirects one http version URL to its equivalent https version. You can add any number of redirections; for example, if you have a lot of secure form URLs in the domain, but are using the same template.

    In this case, just add another if statement right below the first redirection. See the example below:

    <?php

    function url() {

    $urlofthepage = 'http';

    if ($_SERVER["HTTPS"] == "on") {$urlofthepage .= "s";}

    $urlofthepage .= "://";

    if ($_SERVER["SERVER_PORT"] != "80") {

    $urlofthepage .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];

    } else {

    $urlofthepage .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

    }

    return $urlofthepage;

    }

    $urlofthepage=url();

    if ($urlofthepage=="http://www.thisistheurlthatyouneedtoredirecttohttpsversion.com/
    securecontactexampleform.php")

    {

    // Permanent redirection

    header("HTTP/1.1 301 Moved Permanently");

    header("Location: https:// www.thisistheurlthatyouneedtoredirecttohttpsversion.com/
    securecontactexampleform.php ");

    exit();

    }

    //this is the second URL to be redirected to its https version

    if ($urlofthepage==
    "http://www.thisisthesecondurlthatyouneedtoredirecttohttpsversion.com/
    securecontactexampleform.php")

    {

    // Permanent redirection

    header("HTTP/1.1 301 Moved Permanently");

    header("Location: https:// www.thisisthesecondurlthatyouneedtoredirecttohttpsversion.com/
    securecontactexampleform.php ");

    exit();

    }

    ?>




     
     
    >>> More PHP Articles          >>> More By Codex-M
     

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek