title: Editing a PHP File, with another php file, using Fwrite
body: <p>My last question wasn't explained very well.</p>
  
  <p>What I'm trying to do here is insert data into a PHP File, Using the fwrite feature on another .php file.</p>
  
  <p>To keep this simple, I'm labelling the one I want data inserted as file.php and the one I'm using fwrite to execute on, is edit.php</p>
  
  <p>Now, I got the writing thing down, what my problem is, is I need to INSERT that data, Before the closing php tag on file.php.</p>
  
  <p>What I tried doing was, deleting the closing php tag, writing the data, and then rewriting the tag.</p>
  
  <p>Here is my source code for that:</p>
  
  <pre><code>&lt;?php
  $rows = file("file.php");    
  $tagremove = "?&gt;";
  foreach($rows as $key =&gt; $row) {
  if(preg_match("/($tagremove)/", $row)) {
      unset($rows[$key]);
  }
  }
  file_put_contents("file.php", implode("", $rows));
  
  $User = $_GET["user"];
  $File = "file.php"; 
  $Handle = fopen($File, "a");
  fwrite($Handle, "");
  fwrite($Handle, $User);
  fwrite($Handle, "\r\n");
  fwrite($Handle, "?&gt;");
  print "Data Written"; 
  fclose($Handle); 
  ?&gt;
  </code></pre>
  
  <p>When I run this on Edit.php, it inserts that data into the file, but its only writing to the first line, and replacing whatever is already there. (In my case its the opening php tag). I don't know what I'm doing wrong, or if there is another way to do this, but any assistance would be appreciated.</p>
  
  <p>Edit: this is again for a chat client.</p>
  
  <p>I'm having a file, that sends a message into a .txt file that the client then reads.</p>
  
  <p>And that file is reading file.php (staff.php) to check if the user submitting is a staff member.</p>
  
  <p>If it comes up true that the user is a staff member, then it changes the username variable in the send.php.</p>
  
  <p>And so far, the send.php has only sucessfully, included the Staff.php, I've tried staff.txt, and the reason is, php code is in the staff.php.</p>