Well - detecting HTTPS is generally straight forward.
The method to use depends on how your PHP has been installed (i.e. as a module, or as standard cgi).
If you use the modular version: -
Just check that
$HTTP_SERVER_VARS["HTTPS"] == 'on'
or, if you don't find that this is set when you are behind HTTPS (do a quick check on what get set on your server behind https), just use
$GLOBALS['SERVER_PORT'] == 443
since the default port for https is 443 (you may have to change this to a different port if your server admin has modified this at all - but this is unlikely).
Hope this helps