Bradley Coudriet trying to make the world better, one line of code at a time.

27Mar/082

Force SSL connection using PHP

When place before all other output on your PHP script this little snipped will force you PHP page to be presented over SSLThis is very useful on things like Login forms.
 
if($_SERVER['SERVER_PORT'] != '443') {
    //Force SSL upon this page
    header("Location: https://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
}
Comments (2) Trackbacks (0)
  1. Why not do it at the Web server using Apache’s mod_rewrite:

    RewriteEngine On
    RewriteCond %{SERVER_PORT} !443$
    RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]

  2. This is just a nice little snippet for users not familiar with mod_rewrite, or those who just want to have a single script go to HTTPS.

    Unlike you and I, not everyone is a mod_rewrite god ;-)


Leave a comment

(required)

No trackbacks yet.