title: Login / Logout using session in php
body: <p>I am working on login logout using php with session concept. Created 4 files</p>
  
  <ol>
  <li>login.php</li>
  <li>checklogin.php</li>
  <li>securepage.php</li>
  <li>logout.php</li>
  </ol>
  
  <p>I'm able to login with defined username and password when i logged in session gets started login.php will navigate to securepage.php if i copy securepage.php file in some other browser it doesnot gets open it will redirect to login.php vch works great. </p>
  
  <p>But if i open securepage.php in same browser in another tab it is redirecting to login.php instead it should show securepage.php can somebody help me out in achieving it Thanks!</p>
  
  <p><strong>1.Login.php</strong></p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Login Form&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h2&gt;Login Form&lt;/h2&gt;
          &lt;form method="post" action="checklogin.php"&gt;
              USER ID: &lt;input type="text" name="uid"/&gt;&lt;br&gt;
              PASSWORD: &lt;input type="password" name="pw"/&gt;&lt;br&gt;
              &lt;input type="submit" value="Login"/&gt;
          &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>2.checklogin.php</strong></p>
  
  <pre><code>&lt;?php 
      $uid = $_POST['uid'];
      $pw = $_POST['pw'];
  
      if($uid == 'arun' and $pw == 'arun123')
      {
          session_start();
          $_SESSION['sid'] = session_id();
          header('location:securepage.php');
      }
  ?&gt;
  </code></pre>
  
  <p><strong>3. securepage.php</strong></p>
  
  <pre><code>&lt;?php 
      session_start();
      if($_SESSION['sid'] == session_id())
      {
          echo "welcome to you&lt;br&gt;";
          echo "&lt;a href='logout.php'&gt;Logout&lt;/a&gt;";
      }
      else
      {
          header('location:login.php');
      }
  ?&gt;
  </code></pre>
  
  <p><strong>4.logout.php</strong></p>
  
  <pre><code>&lt;?php 
      echo "Logged out successfully";
  
      session_start();
      session_destroy();
      setcookie(session_id(),time()-1);
  ?&gt;
  </code></pre>