title: php session timeout all code in one page
body: <p>Regarding to <a href="https://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes">How do I expire a PHP session after 30 minutes?</a>, I copied some code from the 2nd answer <strong>Simple way of PHP session expiry in 30 minutes.</strong> I'd like to combine login and information to 1 page and another page is logout.php
  here is my code. </p>
  
  <p><strong>homepage.php</strong></p>
  
  <pre><code>if(isset($_POST["submitform"])){
  
      $v1 = "admin";
      $v2 = "admin";
      $v3 = $_POST['username'];
      $v4 = $_POST['password'];
  
      if($v1 == $v3 &amp;&amp; $v2 == $v4){
      session_start();
      $_SESSION['username'] = $v1;
      $_SESSION['start'] = time(); // taking now logged in time
      $_SESSION['expire'] = $_SESSION['start'] + (1* 30) ; // ending a session in 30 seconds
  
      if(!isset($_SESSION['username'])){
        echo "Please Login again &lt;a href='logout.php'&gt;Click Here to Login&lt;/a&gt;";
      }else{
         $now = time(); // checking the time now when home page starts
          if($now &gt; $_SESSION['expire']){
           session_destroy();
            echo "Your session has expire !  &lt;a href='logout.php'&gt;Click Here to Login&lt;/a&gt;";
          }else{
          echo "This should be expired in 1 min &lt;a href='logout.php'&gt;Click Here to Login&lt;/a&gt;";
          }
      }
      }else{
       echo '
      &lt;form  method="post"&gt;
      &lt;input type="text" name="username"&gt;
      &lt;input type="password" name="password"&gt;
      &lt;button type="submit" name="submitform"&gt;Sign in&lt;/button&gt;
      &lt;/form&gt;';
      echo  '&lt;font color="red"&gt;wrong password&lt;/font&gt;"';
      }       
   }else{
      echo '
      &lt;form  method="post"&gt;
      &lt;input type="text" name="username"&gt;
      &lt;input type="password" name="password"&gt;
      &lt;button type="submit" name="submitform"&gt;Sign in&lt;/button&gt;
      &lt;/form&gt;';
   }
  ?&gt;
  </code></pre>
  
  <p><strong>Logout.php</strong></p>
  
  <pre><code>&lt;?php
  session_start();
  session_destroy();
  header('Location: homepage.php');
  ?&gt;
  </code></pre>
  
  <p>I set session expire to 30 seconds, however I found the session doesn't expire as expected. The session never expire. I am wondering if i put <code>session_start();</code> in a right place? Thanks</p>