title: PHP: How to check if user is already logged in and otherwise redirect to login page
body: <p>I am new to PHP and am struggling with the following: </p>
  
  <p>I have a page where I want to check if someone is a registered user before letting them see the content of the site. 
  So my thought was that in my header file (which is referenced on all single pages via <code>require_once("includes/header.php");</code>) I can check on that and redirect them to a login page (<code>login.php</code>) if they have not logged yet. </p>
  
  <p><strong>So here is everything that I have in my header:</strong> </p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;?php 
              define("someUnguessableVariable", "anotherUnguessableVariable");
              session_start();
              if(!(isset($_SESSION['login']) &amp;&amp; $_SESSION['login'] != '')){
                  header ("Location: login.php");
              }
  
              include "system/config.php";
  
              $pageURL = basename($_SERVER["REQUEST_URI"]);
              $pageName = pathinfo(parse_url($pageURL, PHP_URL_PATH), PATHINFO_FILENAME); 
  
              $selectedLang = $_GET["lang"];
                  if(!isset($selectedLang)){
                      $selectedLang = "de";
                  }
              $langURL = "?lang=" . $selectedLang;
  
              $conn = new mysqli($dbServer, $dbUser, $dbPass, $dbName);
              $conn-&gt;set_charset("utf8");
              if($conn-&gt;connect_error){
                  die("Connection failed: " . $conn-&gt;connect_error);
              } 
              // fetch main translations
              $location = "%main%";
              $stmt = $conn-&gt;prepare("SELECT tID, " . $selectedLang . " FROM TranslationsMain WHERE location LIKE ? ORDER BY tID");
              $stmt-&gt;bind_param("s", $location);
              $stmt-&gt;execute();
              $result = $stmt-&gt;get_result();  
              while($arrTranslations = $result-&gt;fetch_assoc()){
                  $trans[] = array("ID" =&gt; $arrTranslations["tID"], "trans" =&gt; $arrTranslations[$selectedLang]);
              }
              $conn-&gt;close();
  
              // get main translations by ID
              function fetchTransMain($trans, $itemID){
                  foreach($trans as $key =&gt; $val){
                      if($val["ID"] == $itemID){
                          return $val["trans"];
                      }
                  }
              }
          ?&gt;
  
          &lt;meta charset="utf-8" /&gt;
          &lt;meta http-equiv="X-UA-Compatible" content="IE=edge" /&gt;
          &lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&gt;
          &lt;meta name="author" content="Some author" /&gt;
          &lt;meta name="description" content="Created: 2015-06" /&gt;
  
          &lt;base href="http://www.myurl.de" target="_self" /&gt;
  
          &lt;title&gt;Some title&lt;/title&gt;
  
          &lt;!-- CSS --&gt;        
          &lt;link rel="stylesheet" type="text/css" href="includes/styles.css" /&gt;
          &lt;!-- CSS - Font Awesome --&gt;
          &lt;link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" /&gt;
  
          &lt;!-- include favicon --&gt;
          &lt;link rel="shortcut icon" href="images/favicon/favicon.ico" type="image/x-icon" /&gt;
          &lt;link rel="icon" href="images/favicon/favicon.png" type="image/png" /&gt;
          &lt;link rel="icon" sizes="32x32" href="images/favicon/favicon-32.png" type="image/png" /&gt;
          &lt;link rel="icon" sizes="64x64" href="images/favicon/favicon-64.png" type="image/png" /&gt;
          &lt;link rel="icon" sizes="96x96" href="images/favicon/favicon-96.png" type="image/png" /&gt;
          &lt;link rel="icon" sizes="196x196" href="images/favicon/favicon-196.png" type="image/png" /&gt;
          &lt;link rel="apple-touch-icon" sizes="152x152" href="images/favicon/apple-touch-icon.png" /&gt;
          &lt;link rel="apple-touch-icon" sizes="60x60" href="images/favicon/apple-touch-icon-60x60.png" /&gt;
          &lt;link rel="apple-touch-icon" sizes="76x76" href="images/favicon/apple-touch-icon-76x76.png" /&gt;
          &lt;link rel="apple-touch-icon" sizes="114x114" href="images/favicon/apple-touch-icon-114x114.png" /&gt;
          &lt;link rel="apple-touch-icon" sizes="120x120" href="images/favicon/apple-touch-icon-120x120.png" /&gt;
          &lt;link rel="apple-touch-icon" sizes="144x144" href="images/favicon/apple-touch-icon-144x144.png" /&gt;
          &lt;meta name="msapplication-TileImage" content="favicon-144.png" /&gt;
          &lt;meta name="msapplication-TileColor" content="#ffffff" /&gt;
  
          &lt;script&gt;
              var baseURL = '&lt;?php echo $baseURL; ?&gt;';
              var pageURL = '&lt;?php echo $pageURL; ?&gt;';
              var pageName = '&lt;?php echo $pageName; ?&gt;';
              var selectedLang = '&lt;?php echo $selectedLang; ?&gt;';
          &lt;/script&gt;
      &lt;/head&gt;   
      &lt;body&gt;
  </code></pre>
  
  <p>Now this is not working and I think I am probably missing a couple of things but I couldn't find a good tutorial or guideline on that. 
  Also, I am not sure if there is anything else I need to do in order to start and set up the session. </p>
  
  <p>Can someone help me with this ?</p>
  
  <p><strong>Note:</strong><br>
  This is only about checking if a user is already logged in since all the actual user registration and verification is done on the separate login page and for this I already have the code working. </p>
  
  <p><strong>Update:</strong> 
  Enabling error messages returns the following errors: </p>
  
  <pre><code>Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /homepages/21/d580042014/htdocs/index.php:2) in /homepages/21/d580042014/htdocs/includes/header.php on line 9
  
  Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /homepages/21/d580042014/htdocs/index.php:2) in /homepages/21/d580042014/htdocs/includes/header.php on line 9
  array(0) { } 
  Warning: Cannot modify header information - headers already sent by (output started at /homepages/21/d580042014/htdocs/index.php:2) in /homepages/21/d580042014/htdocs/includes/header.php on line 12
  
  Notice: Undefined index: lang in /homepages/21/d580042014/htdocs/includes/header.php on line 18
  </code></pre>
  
  <p><strong>Update:</strong><br>
  As per the comments I now posted everything that's currently in the header.</p>
  
  <p>Many thanks in advance.</p>