Loading Gif

How to test if your facebook page tab is using https or http protocol using php

05May2013

How to test if your facebook page tab is using https or http protocol using php

I spent hours the other day searching and searching the facebook SDK documentation for the ability to detect the dreaded https mode in facebook custom tabs.

Anyone who has tried to find anything useful or up to date in the Facebook docs will know how hard it can be to find anything of use on the facebook developer pages.

After about half a day of searching and trying multiple methods from the PHP SDK then the JS SDK I decided facebook documentation was crap and I was going to have to find an alternate method.

I then proceeded to search the php ‘signed_request’ variable that gets sent by facebook to all page tabs. I knew I could retrieve information such as whether my page was liked, and the age and id of the user viewing the page from this encoded variable (Read about the signed request variable here) So it seemed logical to me that facebook would also include whether the tab was in secure mode here!

Alas I was wrong and I was still no closer to my goal.

Then I had an Idea! How could I have been so stupid! I had already tried to grab the protocol from the facebook iframe with javascript!

(I know this is stupid because of cross domain policy and probably would never work, but come on its facebook. We all know facebook behaves in the weirdest ways)

Why didn’t I just use the php equivalent and grab the protocol from the $_SERVER variable (See here for info on the $_SERVER var here)

I headed over to my facebook page tab in http mode expecting to see the words ‘HTTP/0.1’… Which I did. At this point I was convinced I had cracked it. As my mouse hovered over the address bar and I edited the url to read https I was certain that once I refreshed the page the words ‘HTTPS/0.1′ would be there waiting for me! I would be filled with joy and treat my self to a victory sausage roll. Oh how much fun I would have!

But no.

Again I was faced with the dreaded ‘HTTP/0.1′ not the ‘HTTPS/0.1′ that I had so hoped for.

This seemed strange to me as surely it should have worked… but as we all know all is not as it seems on facebook

It was now the end of the day and out of pure desperation I thought I would check the contents of the $_SERVER var in php. To do this I simply did:

To my surprise there was a load of information in there that isn’t normally found. Try it yourself on your own page tab and you may be amazed at what you will find.

One value in particular stood out to me. The ‘HTTP_X_FORWARDED_PROTO’ var. I noticed that if the page was being served in http, the value of this var was ‘http’ and low and behold if the page was being served in https the value was ‘https’. This was behaving exactly as I thought the ‘SERVER_PROTOCOL’ var should have.

HOORAR!, My dilemma was solved and I could get on with my day! Which was now over. Time to go home and have a sausage roll methinks.

In summary if you want to know whether you page tab is being served in http or https you can use the following php.