title: How to detect secure connection (https) using PHP?
body: <p>At the moment I have SSL included on my server. Want to force my users to login using login-page via https.</p>
  
  <pre><code>&lt;?php
  
    if (empty($_SERVER['https']) || $_SERVER['https'] != 'on') {
      header('location: https://my_login_page');
      exit();
      }
  
    # rest of code
    ...
  </code></pre>
  
  <p>But it's a problem when there's no SSL.</p>
  
  <p>Now I have situation. User requests following URL</p>
  
  <pre><code>http://domain.com/login.php
  </code></pre>
  
  <p>Here, I can't access to <code>$_SERVER['https']</code> and want to make sure it's possible to redirect user to</p>
  
  <pre><code>https://domain.com/login.php
  </code></pre>
  
  <p>For example, SSL certificate expires after some time and want to keep users using login w/out secure connection.</p>
  
  <p>My goal is something like this example:</p>
  
  <pre><code>if (ssl_installed() &amp;&amp; (empty($_SERVER['https']) || $_SERVER[] != 'on')) {
    header('location: https://domain.com/login.php');
    exit();
    }
  
  # when there's no SSL it continues using HTTP
  </code></pre>
  
  <p>Yes, want to write function (example: <code>ssl_installed()</code>) which returns <code>true</code> when it's possible using secure connection, otherwise <code>false</code>.</p>
  
  <p>I've tried using <code>get_headers()</code> and realized it always returns false against <code>https://</code> links.</p>
  
  <p><strong>Possible solution:</strong></p>
  
  <p>I already have working solution. Config table in my database contains row <code>ssl=1</code> (or 0) and after database connection has been established I'm using this value to decide is there possible to use SSL or not and function mentioned above uses this value to return result.</p>
  
  <p>My question is: <strong>Is there simpler solution?</strong></p>
  
  <p>To be clear: <strong>I'm looking for PHP-ONLY SOLUTION (auto-detection)!</strong></p>
  
  <p>Any help would be appreciated.</p>