title: How to safely use $_SERVER['REMOTE_ADDR'] and $_SERVER['HTTP_USER_AGENT']
body: <p>I'm wondering how safe it is to use this code. Especially the <code>$_SERVER['REMOTE_ADDR']</code> and <code>$_SERVER['HTTP_USER_AGENT']</code></p>
  
  <p>I've been doing some research on this.
  As far as I can tell it should be safe to just compare the variables like I do here?
  I should only worry about their values when using them to run sql queries or when displaying their content on my pages?</p>
  
  <p>Depending on where I use them I should run <code>mysqli_real_escape_string</code> or <code>htmlspecialchars</code>.</p>
  
  <p>When should I use the <code>trim</code> or <code>stripslashes</code> functions?</p>
  
  <p>I'm just trying to protect my website from being hacked in the most obvious/common ways. Doesn't require huge protection, but I'd like it to be at least decently secure!</p>
  
  <p>Anything I'm overlooking or doing wrong?
  Are there any guide that explain most of this?</p>
  
  <pre class="lang-php prettyprint-override"><code>session_start();
  
      if (!isset($_SESSION['logged_in'])) {
              $_SESSION['logged_in'] = false;
              $_SESSION['ip_address'] = $_SERVER['REMOTE_ADDR'];
              $_SESSION['user_agent'] = (isset($_SERVER['HTTP_USER_AGENT'])) ? $_SERVER['HTTP_USER_AGENT'] : '';
      } else {
              if ($_SESSION['ip_address'] !== $_SERVER['REMOTE_ADDR'] || ($_SESSION['user_agent'] !== $_SERVER['HTTP_USER_AGENT'])) {
                      session_destroy();
                      header('Location: view/index.php');
                      die();
              }
      }
  </code></pre>