Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent access to PHP files if the caller isn't using HTTPS?

I have written several PHP web services where I pass in arguments via the URL. To prevent unauthorized access, I pass in a unique key as one of the arguments. I call the PHP file via HTTPS, and I am wondering if there's a way I can prevent the script from running if HTTPS is not used.

like image 361
Quentamia Avatar asked Sep 16 '10 18:09

Quentamia


People also ask

How do I block direct access to a .php page?

The best way to prevent direct access to files is to place them outside of the web-server document root (usually, one level above). You can still include them, but there is no possibility of someone accessing them through an http request.

How check request is http or https in PHP?

Approach 1: Check if the connection is using SSL and if the value of $_SERVER['HTTPS'] is set, then we can say that the connection is secured and called from 'HTTPS'. If the value is empty, this means the value is set to '0' or 'off' then we can say that the connection is not secured and the page is called from 'HTTP'.


2 Answers

Slightly off topic, but if you're using PHP with Apache Httpd and mod_ssl, you can force SSL access to files (and PHP scripts) by placing the SSLRequireSSL directive in .htaccess or in the Directory configuration.

like image 171
Bruno Avatar answered Nov 02 '22 13:11

Bruno


if(empty($_SERVER['HTTPS'])) {
    // ....
    exit;
}
like image 31
Paolo Bergantino Avatar answered Nov 02 '22 11:11

Paolo Bergantino