Look at the output of <?php phpinfo() ?> and you will find a lot of variables which may be used for various purposes.
Never trust the HTTP_REFERER variable; it's set by the user's browser.
To find out if SSL is active, I use the "HTTPS" server environment variable:
if (getenv('HTTPS')=='on'))
print '<p>SSL is active</p>';
else
print '<p>SSL is <em> not active</p>';