title: How to make an page only accessible when logged in PHP
body: <p>Currently I'm working on a little project for a dummy login/register page and now I want to add a page that is only accessible when you're logged in.
  So the question is how do I make a session or cookie and retrieve them?
  And how do I block not logged in users.</p>
  
  <p>I'm currently using these codes for the login.php and member_area.php:
  Login.php:</p>
  
  <pre><code>&lt;?php
      session_start();
  
  
  
  
  
      if(isSet($_POST['login'])) {
          include('db.php');
  
          $username = mysql_real_escape_string($_POST['username']);
          $password = sha1($_POST['password'] );
  
          $query = mysql_query("SELECT * FROM tab WHERE username='".addSlashes($username)."' AND password='".addSlashes($password)."'");
          $res = mysql_num_rows($query);
  
          if ($res == 1) {
              $_SESSION['username'] = $username;
              $_SESSION['password'] = $password;
              $_SESSION['userobj'] = mysql_fetch_assoc($query);
  
              header('Location: http://localhost/member_area.php');
              exit;
          } else {
              echo 'Data does not match &lt;br /&gt; RE-Enter Username and Password';
          }
      } else {
  
  ?&gt;
      &lt;html&gt;
      &lt;head&gt;&lt;link rel="stylesheet" type="text/css" href="css.css"&gt;&lt;/head&gt;
          &lt;body&gt;
                  &lt;div id="div1"&gt;
  
                  &lt;a href="index.php" id="home"&gt;Home&lt;/a&gt;
                  &lt;a href="Login.php" id="login2"&gt;Login&lt;/a&gt;
                  &lt;a href="register.php" id="register"&gt;Register&lt;/a&gt; 
  
  
          &lt;/div&gt;
              &lt;table width="200" border="0" cellspacing="1" align="center"&gt;
                  &lt;form id="form1" method="post" action="login.php"&gt;
                  &lt;tr&gt;
                      &lt;td colspan="2"&gt;&lt;h2&gt;Members login&lt;/h2&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;Username: &lt;/td&gt;
                      &lt;td&gt;
                          &lt;input type="text" name="username" id="username"/&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;Password: &lt;/td&gt;
                          &lt;td&gt;&lt;input type="password" name="password" id="password"/&gt; &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td colspan="2" align="center"&gt;
                          &lt;input type="submit" name="login" id="login" value="login" /&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;/form&gt;
              &lt;/table&gt;
              &lt;/body&gt;
      &lt;/html&gt;
      &lt;?php
      }
  
  
  ?&gt;
  </code></pre>
  
  <p>Member_area.php:</p>
  
  <pre><code>&lt;?php
  
  ?&gt;
  &lt;html&gt;
  &lt;head&gt;&lt;link rel="stylesheet" type="text/css" href="css.css"&gt;&lt;/head&gt;
      &lt;body&gt;
              &lt;div id="div1"&gt;
  
                  &lt;a href="index.php" id="home"&gt;Home&lt;/a&gt;
                  &lt;a href="Login.php" id="login2"&gt;Login&lt;/a&gt;
                  &lt;a href="register.php" id="register"&gt;Register&lt;/a&gt; 
  
  
          &lt;/div&gt;
          &lt;form action="/Log_out.php" method="get"&gt;
              &lt;input type="submit" name="submit" value="Log Out." action="http://localhost/Log_out.php" id="Logout"&gt;
          &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  &lt;?php
  
  ?&gt;
  </code></pre>
  
  <p>Please note that I'm completely new to PHP so some directions where to put the code with if possible a little explanation. </p>