Jump to content

Writing to next text-file-line problem


SwarleyAUS

Recommended Posts

hey guys

 

coding a website atm that allows you to, once youve registered, change your account details. the user data is stored in a text file line by line, with each user having a varying number of characters for username, password, name, surname, phone number, email etc. these details are all separated by "|".

 

my problem is that when the user adjusts their information, the line their data is on in the text file either doesnt get overridden fully (if their new input of characters is less than originally) and the old data is pushed to the new line because of a "\n", or it overwrites information on the next line (if their new input of characters is more than originally) and then does a new line.

 

tried to do a cutline program, but thats a bit beyond me

 

thanks guys

 

 

CODE:

 

function cutline($file, $line_no =-1) { //not working but im guessing itd be something along those lines

 

$strip_return = false;

$size = count($data);

 

if($line_no == -1) {

$skip = $size-1;

} else {

$skip = $line_no-1;

}

 

for($line = 0; $line < $size; $line++) {

if($line != $skip) {

fputs($file, $data[$line]);

} else {

$strip_return = true;

}

}

return $strip_return;

}

 

if (isset($_POST['adjust'])) {

$file = fopen("members.txt", 'r+');

while (!feof($file)) {

if ($data[0] == $_SESSION['type'] && $data[1] == $_SESSION['password'] && $data[5] == $_SESSION['ph'] && $data[7] == $_SESSION['email']) {

echo "found user";

if ($_POST['new_username'] == "") {

$new_username = $_SESSION['type'];

} else {

$new_username = $_POST['new_username'];

$_SESSION['type'] = $new_username;

}

 

if ($_POST['new_password'] == "") {

$new_password = $_SESSION['password'];

} else {

$new_password = $_POST['new_password'];

$_SESSION['password'] = $new_password;

}

 

if ($_POST['new_ph'] == "") {

$new_ph = $_SESSION['ph'];

} else {

$new_ph = $_POST['new_ph'];

$_SESSION['ph'] = $new_ph;

}

 

if ($_POST['new_address'] == "") {

$new_address = $_SESSION['address'];

} else {

$new_address = $_POST['new_address'];

$_SESSION['address'] = $new_address;

}

 

if ($_POST['new_email'] == "") {

$new_email = $_SESSION['email'];

} else {

$new_email = $_POST['new_email'];

$_SESSION['email'] = $new_email;

}

 

fseek($file, $line);

cutline($file, $line);

$data = fwrite($file, $new_username."|".$new_password."|".$_SESSION['name']."|".$_SESSION['surname']."|".$_SESSION['dob']."|".$new_ph."|".$new_address."|".$new_email."|".$_POST['new_reglevel']."|".$_POST['new_regtime']."\n");

fclose($file);

exit();

} else {

  $line+1;

}

} fclose($file);

}

 

 

TEXT FILE (members.txt):

 

jimBob|jimBob|Jim|Bob|20/02/1965|95555555|Mentone|[email protected]|Gold|1 Year

admin|123|admin|admin1|-|-|-|[email protected]|-|-

blah|blah|-|-|-|-|Gold|Lifetime

 

 

eg - TEXT FILE after user data changed and less characters are inputted:

 

jim|abc|Jim|Bob|20/02/1965|95555555|blahaha|[email protected]|Gold|1 Year

ar

admin|123|admin|admin1|-|-|-|[email protected]|-|-

blah|blah|-|-|-|-|Gold|Lifetime

 

 

Link to comment
https://forums.phpfreaks.com/topic/201930-writing-to-next-text-file-line-problem/
Share on other sites

Load the file into memory, then use a regular expression to find the username and that particular line, then replace the contents using preg_replace() or preg_replace_callback() and save it back again

This would be achieved a lot easier and more reliably with MySQL however

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.