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

Checking also connection port as on some environments $_SERVER['HTTPS'] can be empty
can you add a strtolower() on $_SERVER['HTTPS'] as some servers may return On or ON