I've got just one page that I want to force to be accessed as an HTTPS page (PHP on Apache). How do I do this without making the whole directory require HTTPS? Or, if you submit a form to an HTTPS page from an HTTP page, does it send it by HTTPS instead of HTTP?
Here is my example:
http://www.example.com/some-page.php
I want it to only be accessed through:
https://www.example.com/some-page.php
Sure, I can put all of the links to this page pointed at the HTTPS version, but that doesn't stop some fool from accessing it through HTTP on purpose...
One thing I thought was putting a redirect in the header of the PHP file to check to be sure that they are accessing the HTTPS version:
if($_SERVER["SCRIPT_URI"] == "http://www.example.com/some-page.php"){
header('Location: https://www.example.com/some-page.php');
}
But that can't be the right way, can it?
BTW, please pay no attention to the URL. I know that if it were actually a page where there was a shopping cart, etc., you would do it a different way. Think of it as a page from a site that sells one item for one price where you type in your credit card info to be submitted to a payment gateway on an external site for the express purpose of charging your card one time.
use
htaccess
:maybe this one can help, you, that's how I did for my website, it works like a charm :
The way I've done it before is basically like what you wrote, but doesn't have any hardcoded values:
I have been through many solutions with checking the status of $_SERVER[HTTPS] but seems like it is not reliable because sometimes it does not set or set to on, off, etc. causing the script to internal loop redirect.
Here is the most reliable solution if your server supports $_SERVER[SCRIPT_URI]
Please note that depending on your installation, your server might not support $_SERVER[SCRIPT_URI] but if it does, this is the better script to use.
You can check here: Why do some PHP installations have $_SERVER['SCRIPT_URI'] and others not
Using this is NOT enough:
If you have any http content (like an external http image source), the browser will detect a possible threat. So be sure all your ref and src inside your code are https
Ok.. Now there is tons of stuff on this now but no one really completes the "Secure" question. For me it is rediculous to use something that is insecure.
Unless you use it as bait.
$_SERVER propagation can be changed at the will of someone who knows how.
Also as Sazzad Tushar Khan and the thebigjc stated you can also use httaccess to do this and there are a lot of answers here containing it.
Just add:
to the end of what you have in your .httaccess and thats that.
Still we are not as secure as we possibly can be with these 2 tools.
The rest is simple. If there are missing attributes ie...
Also say you have updated your httaccess file and you check:
There are a lot more variables you can check ie..
HOST_URI
(If there are static atributes about it to check)HTTP_USER_AGENT
(Same session different values)So all Im saying is dont just settle for one or the other when the answer lies in a combination.
For more httaccess rewriting info see the docs-> http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
Some Stacks here -> Force SSL/https using .htaccess and mod_rewrite
and
Getting the full URL of the current page (PHP)
to name a couple.