title: PHP Session between pages behaving erratically
body: <p>I have a a 'login' page in my site handled by a 'login-act' script which redirects to a 'post' page on success. The 'post' page has links to post various types of content, e.g. 'post-audio'. The 'post' page works fine as it displays user name if authenticated, but from then on it's disaster: if an authenticated user clicks on 'post-audio', somehow, it logs him out and redirects him to the login page. But then, after some time, (or if i make and undo a change in the 'post-audio' script) it works fine again. It's driving me nuts. Can you help?</p>
  
  <p>login-act.php:</p>
  
  <pre><code>&lt;? ob_start();//Start buffer output ?&gt;
  
  &lt;html&gt;
  
  &lt;head&gt;
  &lt;link rel="stylesheet" type="text/css" href="mystyle-a.css"&gt;
  &lt;title&gt;BQuotes CMS: User Generated Content: Login Notification&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body class='center'&gt;
  
  &lt;?php
  session_start();
  if(isset($_POST["captcha"])&amp;&amp;$_POST["captcha"]!=""&amp;&amp;$_SESSION["code"]==$_POST["captcha"])
  {
  // echo "&lt;font color='green'&gt;Correct Code Entered";
  
  //Do req
  
  
  
  
  
  $host="host"; // Host name 
  $username="user"; // Mysql username 
  $password="password"; // Mysql password 
  $db_name="db"; // Database name 
  $tbl_name="table"; // Table name 
  $tbl_name2="table2"; // Table name 2
  
  // Connect to server and select database.
  mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
  mysql_select_db("$db_name")or die("cannot select DB");
  
  // Get values from form 
  $myusername=mysql_real_escape_string($_POST['myusername']);
  $mypassword=mysql_real_escape_string($_POST['mypassword']);
  
  // Validate the login
  $sql2="SELECT * FROM $tbl_name2 WHERE username='$myusername' and password='$mypassword'";
  $result2=mysql_query($sql2);
  
  $count=mysql_num_rows($result2);
  
  // If result matched $myusername and $mypassword, table row must be 1 row
  if($count==1)
               {
  session_start();             
  $_SESSION['myusername'] = $myusername;
  header ("Location: mybq-post.php");
  
               }
  
  else {
  echo "&lt;div class='center2'&gt;&lt;font color='red'&gt;Invalid Login Details. Not Logged In.&lt;/div&gt;";
  echo "&lt;br&gt;";
  echo "&lt;div class='center2'&gt;&lt;font color='red'&gt;Please go back and try again.&lt;/div&gt;";
  echo "&lt;br&gt;";
  
  echo "&lt;div class='center2'&gt;&lt;a href='mybq-login.php'&gt;Back&lt;/a&gt;&lt;/div&gt;";
  }
  
  
  }
  
  else {
  echo "&lt;div class='center2'&gt;&lt;font color='red'&gt;Wrong Captcha. Not Logged In.&lt;/div&gt;";
  echo "&lt;br&gt;";
  echo "&lt;div class='center2'&gt;&lt;font color='red'&gt;Please go back and try again.&lt;/div&gt;";
  echo "&lt;br&gt;";
  
  echo "&lt;div class='center2'&gt;&lt;a href='mybq-login.php'&gt;Back&lt;/a&gt;&lt;/div&gt;";
  }
  ?&gt;
  
  
  &lt;?php 
  // close connection 
  //mysql_close();
  ?&gt;
  
  
   &lt;/body&gt; &lt;/html&gt;
  &lt;? ob_flush();//Flush buffer output ?&gt;
  </code></pre>
  
  <p>post.php:</p>
  
  <pre><code>&lt;?php
  
  session_start();
  
  if (!(isset($_SESSION['myusername']) &amp;&amp; $_SESSION['myusername'] != '')) {
  
  header ("Location: mybq-login.php");
  
  }
  
  if ($_SESSION['timeout'] + 10 * 60 &lt; time()) {
  // session timed out
  session_destroy();
  //header("Location: mybq-logout.php");
    }
  
  $_SESSION['timeout'] = time();
  
  
  echo "&lt;body class='left'&gt;&lt;header&gt;&lt;a href='mybq-logout.php'&gt;Logout&lt;/a&gt;&lt;/header&gt;&lt;/body&gt;" . $_SESSION['myusername'];
  
  ?&gt;
  
  &lt;html&gt;
  
  &lt;head&gt;
  &lt;link rel="stylesheet" type="text/css" href="mystyle-a.css"&gt;
  &lt;title&gt;BQuotes CMS: User Generated Content: Post Index&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body class='center'&gt;
  
  &lt;div class='center2'&gt;
  &lt;b&gt;MyBQuotes Post&lt;/b&gt;&lt;br&gt;
  &lt;a href='mybq-post-txt.php'&gt;Post Text&lt;/a&gt; &lt;a href='mybq-post-img.php'&gt;Post Image&lt;/a&gt;&lt;br&gt;
  &lt;a href='mybq-post-audio.php'&gt;Post Audio&lt;/a&gt; &lt;a href='mybq-post-video.php'&gt;Post Video&lt;br&gt;
  &lt;a href='index.php'&gt;CMS Index&lt;/a&gt; &lt;a href='mybq-index.php'&gt;MyBQuotes Main&lt;/a&gt;&lt;br&gt;
  &lt;font size="0.5px;" color="red"&gt;&lt;b&gt;Disclaimer: &lt;/b&gt;Poster solely responsible for posted content!
  &lt;/div&gt;
  
   &lt;/body&gt; &lt;/html&gt;
  </code></pre>
  
  <p>post-audio.php:</p>
  
  <pre><code>&lt;?php
  
  session_start();
  
  if (!(isset($_SESSION['myusername']) &amp;&amp; $_SESSION['myusername'] != '')) {
  
  header ("Location: mybq-login.php");
  
  }
  
  if ($_SESSION['timeout'] + 10 * 60 &lt; time()) {
  // session timed out
  session_destroy();
  //header("Location: mybq-logout.php");
    }
  
  $_SESSION['timeout'] = time();  
  
  
  echo "&lt;body class='left'&gt;&lt;header&gt;&lt;a href='mybq-logout.php'&gt;Logout&lt;/a&gt;&lt;/header&gt;&lt;/body&gt;" . $_SESSION['myusername'];
  ?&gt;
  
  &lt;html&gt;
  
  &lt;head&gt;
  &lt;link rel="stylesheet" type="text/css" href="mystyle-a.css"&gt;
  &lt;title&gt;BQuotes CMS: User Generated Content: Post Audio&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;div class='center2'&gt;
  &lt;body class='center'&gt;
  &lt;b&gt;MyBQuotes Post Audio:&lt;/b&gt;&lt;br&gt;
  &lt;font size=2&gt;Allowed File Type: MP3&lt;br /&gt;
  Max File Size: 8MB&lt;/p&gt;
  &lt;form name=mybq-post-audio action="mybq-post-audio-act.php" method="post" enctype="multipart/form-data"&gt;
  
  &lt;!--
  Username:&lt;br /&gt;
  &lt;input type="text" size="25" name="myusername" /&gt;&lt;br /&gt;
  Password:&lt;br /&gt;
  &lt;input type="password" size="25" name="mypassword" /&gt;&lt;br /&gt;
  --&gt;
  
  Audio:&lt;br /&gt;
  &lt;input type="file" name="audio" id="myaudio" /&gt;&lt;br /&gt;
  Tag:&lt;br /&gt;
  &lt;input type="text" size="25" name="mytag" /&gt;&lt;br /&gt;
  
  Enter Image Text:&lt;br /&gt;
  &lt;input name="captcha" type="text"&gt;
  &lt;img src="captcha.php" /&gt;&lt;br&gt;
  
  &lt;input type="submit" value="Post" /&gt;&lt;br /&gt;
  &lt;/form&gt;
  &lt;a href="forg-pass.htm"&gt;&lt;div class='tagtext'&gt;Forgot Login details?&lt;/a&gt;
  &lt;br /&gt;
  &lt;a href="index.php"&gt;CMS Index&lt;/a&gt; &lt;a href="mybq-post.php"&gt;MyBQuotes Post&lt;/a&gt;
  
  &lt;/div&gt;
   &lt;/body&gt; &lt;/html&gt;
  </code></pre>
  
  <p>Any help is appreciated.
  (I know some of my code is deprecated... i'm working on it :) )</p>