PHP: Function to force HTTPS

June 3, 2010

Quite often HTTPS is required for security reasons, but the question is how to force HTTPS with PHP?  Really it is quite simple. Copy the following function into your functions file and call it on top of every page that requires HTTPS to ensure it is used.

function forceHttps() {
 $pageURL = 'http';
 if (!isset($_SERVER["HTTPS"]) || $_SERVER["HTTPS"] != "on") {
	header("Location: https://" . $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);
Leave a Reply

Your email address will not be published. Required fields are marked *