Change getenv('HTTPS') to $_SERVER['HTTPS']

Checking also connection port as on some environments $_SERVER['HTTPS'] can be empty