title: $_SESSION not seeming to work
body: <p>I have a php script that should detect the <code>$_SESSION['Username']</code> and fwrite it into a txt file. I have a similar script running on my site but for a different use and it is working just fine. As you can see I have </p>
  
  <pre><code>$nickname = $_SESSION['Username']; 
  </code></pre>
  
  <p>and </p>
  
  <pre><code>fwrite(fopen('chat.txt', 'a'), "". $nickname . ": " . $message = str_replace("\n", " ", $message) . "\n"); 
  </code></pre>
  
  <p>I think it is somewhere in here where it is failing.
  Not sure why this one is failing, any and all help would be much appreciated!</p>
  
  <pre><code>&lt;?php
  
      $function = $_POST['function'];
  
      $log = array();
  
      switch($function) {
  
           case('getState'):
               if(file_exists('chat.txt')){
                 $lines = file('chat.txt');
               }
               $log['state'] = count($lines); 
               break; 
  
           case('update'):
              $state = $_POST['state'];
              if(file_exists('chat.txt')){
                 $lines = file('chat.txt');
               }
               $count =  count($lines);
               if($state == $count){
                   $log['state'] = $state;
                   $log['text'] = false;
  
                   }
                   else{
                       $text= array();
                       $log['state'] = $state + count($lines) - $state;
                       foreach ($lines as $line_num =&gt; $line)
                         {
                             if($line_num &gt;= $state){
                           $text[] =  $line = str_replace("\n", "", $line);
                             }
  
                          }
                       $log['text'] = $text; 
                   }
  
               break;
  
           case('send'):
            $nickname = $_SESSION['Username'];
               $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
                $message =($_POST['message']);
           if(($message) != "\n"){
  
               if(preg_match($reg_exUrl, $message, $url)) {
                  $message = preg_replace($reg_exUrl, '&lt;a href="'.$url[0].'" target="_blank"&gt;'.$url[0].'&lt;/a&gt;', $message);
                  } 
  
  
               fwrite(fopen('chat.txt', 'a'), "". $nickname . ": " . $message = str_replace("\n", " ", $message) . "\n"); 
           }
               break;
  
      }
  
      echo json_encode($log);
  
  ?&gt;
  </code></pre>
  
  <h2>Login script that starts session</h2>
  
  <pre><code>&lt;?php
  }  
  elseif(!empty($_POST['username']) &amp;&amp; !empty($_POST['password']))  
  {
  $username = mysql_real_escape_string($_POST['username']);  
      $password = md5(mysql_real_escape_string($_POST['password']));  
  
      $checklogin = mysql_query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'");  
  
      if(mysql_num_rows($checklogin) == 1)  
      {  
          $row = mysql_fetch_array($checklogin);  
          $email = $row['EmailAddress'];  
  
          $_SESSION['Username'] = $username;  
          $_SESSION['EmailAddress'] = $email;  
          $_SESSION['LoggedIn'] = 1;  
  
          echo "&lt;h1&gt;Success&lt;/h1&gt;";  
          echo "&lt;p&gt;We are now redirecting you to the member area.&lt;/p&gt;";  
          echo "&lt;meta http-equiv='refresh' content='=10;index.php' /&gt;";  
      }  
  else  
      {  
          echo "&lt;h1&gt;Error&lt;/h1&gt;";  
          echo "&lt;p&gt;Sorry, your account could not be found. Please &lt;a href=\"index.php\"&gt;click here to try again&lt;/a&gt;.&lt;/p&gt;";  
      }  
  }  
  else  
  {  
      ?&gt;  
  </code></pre>