WP-Mix

A fresh mix of code snippets and tutorials

PHP Check if Request is HTTPS

Here we have a simple and effective PHP function that checks whether or not HTTPS (via SSL) is used for the current request.

function shapeSpace_check_https() {
	
	if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443) {
		
		return true; 
	}
	return false;
}

This function checks the value of the $_SERVER['HTTPS'] variable to see if it contains a non-empty value that is not equal to off. If both of these conditions are true, OR if the $_SERVER['SERVER_PORT'] variable is set to 443, the function evaluates to true. So we can use this function like so:

if (shapeSpace_check_https()) {

	// do something if HTTPS/SSL enabled
} else {

	// do something if HTTPS/SSL not enabled
}

This bit of logic basically checks the value of shapeSpace_check_https() so that specific functionality may be applied in either case (i.e., HTTPS on or HTTPS off). Easy peasy!

★ Pro Tip:

USP ProSAC Pro