{"id":547056,"date":"2010-04-29T03:00:00","date_gmt":"2010-04-29T07:00:00","guid":{"rendered":"tag:blogger.com,1999:blog-1652419620964346731.post-176519025340391555"},"modified":"2010-04-29T03:00:00","modified_gmt":"2010-04-29T07:00:00","slug":"how-to-force-ssl-using-php","status":"publish","type":"post","link":"https:\/\/mereja.media\/index\/547056","title":{"rendered":"How To Force SSL Using PHP"},"content":{"rendered":"<p>I mentioned a few days ago using <a href=\"http:\/\/osticket.com\">osTicket<\/a>, we have been using it for a while. We also have to make it public facing so our customers can use it, and so we can use it from outside of the office. The problem is that since we are using our AD credentials to login there is a major security concern since by default, osTicket is not encrypted. We opted to use <a href=\"http:\/\/godaddy.bauer-power.net\">SSL encryption<\/a> on our ticket system. <\/p>\n<p>No big deal right? Well, we also want to make it so users don\u2019t have to remember to type in the <strong><em>http<font color=\"#ff0000\">S<\/font><\/em><\/strong><font color=\"#ff0000\"> <\/font>part in the address. We want them to be able to type <strong><em>support.companydomain.com<\/em><\/strong> and have it automagically go to our ticket system. Likewise, on the admin page we want to make it so that when you go to <strong><em>support.companydomain.com\/admin<\/em><\/strong> it automagically gets SSL encryption too. One way to do it is to drop an index.html file in with a redirect, that works ok too, but what if you want to ensure that if the <strong><font color=\"#ff0000\">S<\/font><\/strong> in <strong><em>http<font color=\"#ff0000\">S<\/font><\/em><\/strong> is removed, users still get forced to use SSL without any errors? Well in this case I used a little PHP magic.<\/p>\n<p>I created a file called <strong><em>encrypt.php<\/em><\/strong> with the following code:<\/p>\n<blockquote>\n<p><strong><font color=\"#0000ff\">&lt;?          <br \/>function secure_page()           <br \/>{           <br \/>if ( !isset($_SERVER[&#8216;HTTPS&#8217;]) || strtolower($_SERVER[&#8216;HTTPS&#8217;]) !== &#8216;on&#8217; )           <br \/>{           <br \/>header (&#8216;Location: <\/font><\/strong><a href=\"https:\/\/'\"><font color=\"#0000ff\">https:\/\/&#8217;<\/font><\/a><strong><font color=\"#0000ff\">.$_SERVER[&#8216;HTTP_HOST&#8217;].$_SERVER[&#8216;REQUEST_URI&#8217;]);          <br \/>exit();           <br \/>}           <br \/>}           <br \/>secure_page();           <br \/>?&gt;<\/font><\/strong><\/p>\n<\/blockquote>\n<p><a href=\"http:\/\/lh5.ggpht.com\/_jLaEIqL6T8Y\/S9inIer7LbI\/AAAAAAAACYc\/yFCM-E4sBTg\/s1600-h\/php-logo%5B8%5D.jpg\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px\" title=\"php\" border=\"0\" alt=\"php\" align=\"right\" src=\"http:\/\/lh6.ggpht.com\/_jLaEIqL6T8Y\/S9inIrMbXDI\/AAAAAAAACYg\/45EDvKHcSHc\/php-logo_thumb%5B6%5D.jpg?imgmax=800\" width=\"261\" height=\"248\" \/><\/a> On any pages where I wanted to ensure SSL, I added the following line:<\/p>\n<blockquote>\n<p><strong><font color=\"#0000ff\">require(&#8216;encrypt.php&#8217;);<\/font><\/strong><\/p>\n<\/blockquote>\n<p>Similarly, in our old ticket system (Which we are upgrading today now that <a href=\"http:\/\/www.ubuntu.com\">Ubuntu 10.04<\/a> is out!) we added a custom <a href=\"http:\/\/recaptcha.net\/\">reCaptcha<\/a> on the ticket request page. Since we weren&#8217;t hosting the reCaptcha ourselves we couldn\u2019t encrypt it with our SSL cert, and users would get prompted if they wanted to display the unsecure items. That confused people, so we wanted to make sure that page was not encrypted with SSL. <\/p>\n<p>To do that, we did the same as above except this time we created a file called <strong><em>decrypt.php<\/em><\/strong> with the following code:<\/p>\n<blockquote>\n<p><strong><font color=\"#0000ff\">&lt;?          <br \/>function unsecure_page()           <br \/>{           <br \/>if ( isset($_SERVER[&#8216;HTTPS&#8217;]) || strtolower($_SERVER[&#8216;HTTPS&#8217;]) == &#8216;on&#8217; )           <br \/>{           <br \/>header (&#8216;Location: <\/font><\/strong><a href=\"http:\/\/'\"><font color=\"#0000ff\">http:\/\/&#8217;<\/font><\/a><strong><font color=\"#0000ff\">.$_SERVER[&#8216;HTTP_HOST&#8217;].$_SERVER[&#8216;REQUEST_URI&#8217;]);          <br \/>exit();           <br \/>}           <br \/>}           <br \/>unsecure_page();           <br \/>?&gt;<\/font><\/strong><\/p>\n<\/blockquote>\n<p>And once again on that page we added the following line:<\/p>\n<blockquote>\n<p><strong><font color=\"#0000ff\">require(&#8216;decrypt.php&#8217;);<\/font><\/strong><\/p>\n<\/blockquote>\n<p>Done, now on all the pages we want to be encrypted, it is encrypted, and on the pages we don\u2019t want to encrypt it isn\u2019t. Luckily in the new version of osTicket <a href=\"http:\/\/en.wikipedia.org\/wiki\/CAPTCHA\">captcha<\/a> is built in, so I can encrypt all pages without issue. <\/p>\n<p>In both cases, since we wrote those files we can include them on any page we want, including on our <a href=\"http:\/\/www.phpmyadmin.net\/home_page\/index.php\">phpMyAdmin<\/a> page! Boom!<\/p>\n<p>Do you use this method to force SSL on your <a href=\"http:\/\/en.wikipedia.org\/wiki\/LAMP_%28software_bundle%29\">LAMP<\/a> servers? If not, what do you do? Let us know in the comments.<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px\" id=\"scid:0767317B-992E-4b12-91E0-4F059A8CECA8:9b37c492-2380-4f30-a967-29a2fa742997\" class=\"wlWriterEditableSmartContent\">Technorati Tags: <a href=\"http:\/\/technorati.com\/tags\/how+to\" rel=\"tag\">how to<\/a>,<a href=\"http:\/\/technorati.com\/tags\/force\" rel=\"tag\">force<\/a>,<a href=\"http:\/\/technorati.com\/tags\/ssl\" rel=\"tag\">ssl<\/a>,<a href=\"http:\/\/technorati.com\/tags\/php\" rel=\"tag\">php<\/a>,<a href=\"http:\/\/technorati.com\/tags\/apache\" rel=\"tag\">apache<\/a>,<a href=\"http:\/\/technorati.com\/tags\/lamp\" rel=\"tag\">lamp<\/a><\/div>\n<div class=\"blogger-post-footer\"><Br \/><center><a href=\"http:\/\/www.anrdoezrs.net\/click-3773910-10438541?cm_mmc=CJ-_-2769020-_-3773910-_-Home_468x60-Anim_8-24-2006\" ><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.ftjcfx.com\/image-3773910-10438541\" width=\"468\" height=\"60\" alt=\"Great Deals @ Geeks.com!\" border=\"0\"\/><\/a><\/center><img width='1' height='1' src='https:\/\/blogger.googleusercontent.com\/tracker\/1652419620964346731-176519025340391555?l=www.bauer-power.net' alt='' \/><\/div>\n<div class=\"feedflare\">\n<a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=Uai8lLj4J5I:Xpx0PRVIl_g:yIl2AUoC8zA\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?d=yIl2AUoC8zA\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=Uai8lLj4J5I:Xpx0PRVIl_g:dnMXMwOfBR0\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?d=dnMXMwOfBR0\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=Uai8lLj4J5I:Xpx0PRVIl_g:V_sGLiPBpWU\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?i=Uai8lLj4J5I:Xpx0PRVIl_g:V_sGLiPBpWU\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=Uai8lLj4J5I:Xpx0PRVIl_g:gIN9vFwOqvQ\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?i=Uai8lLj4J5I:Xpx0PRVIl_g:gIN9vFwOqvQ\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=Uai8lLj4J5I:Xpx0PRVIl_g:YwkR-u9nhCs\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?d=YwkR-u9nhCs\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=Uai8lLj4J5I:Xpx0PRVIl_g:F7zBnMyn0Lo\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?i=Uai8lLj4J5I:Xpx0PRVIl_g:F7zBnMyn0Lo\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=Uai8lLj4J5I:Xpx0PRVIl_g:wF9xT3WuBAs\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?i=Uai8lLj4J5I:Xpx0PRVIl_g:wF9xT3WuBAs\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=Uai8lLj4J5I:Xpx0PRVIl_g:jTJX5pvEcCQ\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?i=Uai8lLj4J5I:Xpx0PRVIl_g:jTJX5pvEcCQ\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=Uai8lLj4J5I:Xpx0PRVIl_g:TzevzKxY174\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?d=TzevzKxY174\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=Uai8lLj4J5I:Xpx0PRVIl_g:qj6IDK7rITs\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?d=qj6IDK7rITs\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=Uai8lLj4J5I:Xpx0PRVIl_g:7Q72WNTAKBA\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?d=7Q72WNTAKBA\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=Uai8lLj4J5I:Xpx0PRVIl_g:nHK9nIK91uw\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?i=Uai8lLj4J5I:Xpx0PRVIl_g:nHK9nIK91uw\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=Uai8lLj4J5I:Xpx0PRVIl_g:tdI4wLSbWXI\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?i=Uai8lLj4J5I:Xpx0PRVIl_g:tdI4wLSbWXI\" border=\"0\"><\/img><\/a>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~r\/Bauer-power\/~4\/Uai8lLj4J5I\" height=\"1\" width=\"1\"\/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I mentioned a few days ago using osTicket, we have been using it for a while. We also have to make it public facing so our customers can use it, and so we can use it from outside of the office. The problem is that since we are using our AD credentials to login there [&hellip;]<\/p>\n","protected":false},"author":1521,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-547056","post","type-post","status-publish","format-standard","hentry","category-news"],"_links":{"self":[{"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/posts\/547056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/users\/1521"}],"replies":[{"embeddable":true,"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/comments?post=547056"}],"version-history":[{"count":0,"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/posts\/547056\/revisions"}],"wp:attachment":[{"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/media?parent=547056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/categories?post=547056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/tags?post=547056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}