Jump to content

problem with fwrite and $_POST


Digibit

Recommended Posts

I have the following that is supposed to get data from a from and then depending on whether a user is logged in, either create a new account line in the file or update that users information in the file.

 

 

Everything works as expected except when I pass the data through $_POST, the fwrite seems to lose the $_POST data for the account update but retains all the other variables. If I echo the $fileLines[$j] before and after the fwrite statement, the data is correct but not when written to the file.

 

echoing to the screen before and after the fwrite will look like:

 

Before write ->0|username|passwrd|[email protected]|y|firstName|lastName

After Write ->0|username|passwrd|[email protected]|y|firstName|lastName

 

but what will actuallly be written is:

...

0||passwrd||n|||

...

 

For the new account situation, it writes the $_POST data but then it seems to re-request the page - $count is incremented - and continue with another fwrite for a blank account.  Which results in a file entry like this:

 

...

3|username|passwd|[email protected]|Y|firstName|LastName

4||passwd||n|||

...

 

The above will be generated in the file with one fwrite statement.  There is no loop.

 

And to top it off, If I change everything to $_GET and set the form method to get, everything works perfectly.  So am I to assume that there is probably something wrong in my php.ini ?

 

Any help would greatly be appreciated.

 

Here's my code: (PHP.ini is attached as .zip)

 

 

<?php
						$myLoginFile = "myFile.txt";
						$infh = fopen ($myLoginFile,'r');
						$fileLines = array();
						$count=0;
						$subscri="n";
						$dataLine = fgets($infh);
						$userId = $_SESSION['userId'];
						$userName = $_GET['username'];
						$userEmail = $_GET['email'];
						$userFname = $_GET['fname'];
						$userLname = $_GET['lname'];
						$userPass = $_GET['password1'];


						//Read account file, load users into array and count number of users.
						while ($dataLine != null){
							$fileLines[$count] = $dataLine;
							$count++;
							$dataLine = fgets($infh);
						}
						fclose($infh);

						//Check to see if subscription box is checked
						if ($_GET['subscri'] != null){
							$subscri ="y";
						}
					echo '<h1>Thank you!</h1>';							

					//Do this for update
					if ($_SESSION['userId'] != null){

						//find the right user and update info from POST.
						for ($i=0; $i < sizeof($fileLines); $i++){
							$userDetail = explode ("|",$fileLines[$i]);
							if ($userDetail[0] == $_SESSION['userId']){
								$fileLines[$i] = $userId."|".$userName."|".$userDetail[2]."|".$userEmail."|".$subscri."|".$userFname."|".$userLname."\n";
							}
						}
						//overwrite all the file lines back to the file.
						$updateAcctfh = fopen ($myLoginFile,'w');
						for ($j=0; $j< sizeof($fileLines); $j++){
							$newstring = $fileLines[$j];

							echo '<br />Before write ->'.$fileLines[$j];
							fwrite($updateAcctfh,$newstring); // Problem here - output is: "4||passwd||n|||"
							echo "<br />After Write ->".$newstring;	

						}
						fclose($updateAcctfh);

						echo '<p>Your account has been updated with the following information:</p>';

				}else{

						//Do this for new account
						$addNewfh = fopen ($myLoginFile,'a');
						echo 'FirstName: '.$userFname;
						/*Problem here - Output is:
							3|username|passwd|[email protected]|Y|firstName|LastName
							4||passwd||n|||
						*/
						fwrite($addNewfh,$count."|".$userName.'|'.$userFname."|".$userPass."|".$userEmail."|".$subscri."|".$userFname."|".$userLname."\n"); 
						fclose($addNewfh);

						echo '<p>Your account has been created with the following information:</p>';

					}

				echo '	<table>';
				echo '		<tr><td>Username:'.$userName.'</td></tr>';
				echo '		<tr><td>Email:'.$userEmail.'</td></tr>';
				echo '		<tr><td>First Name:'.$userFname.'</td></tr>';
				echo '		<tr><td>Last Name:'.$userLname.'</td></tr>';
				echo '		<tr><td><input type="checkbox" id="subscr" readonly="readonly" name="subscri"';

				if($subscri=="y"){
				echo 'checked="checked"';
				} 
				echo '	/> - Yes, subscribe to newsletter!</td></tr>';
				echo '	</table>';
					?>

 

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/179383-problem-with-fwrite-and-_post/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.