rch10007
04-01-2006, 03:57 AM
I have a text file to store passwords; it's laid out like:
name,password(encrypted with md5)
name,pass...
name,pass..
etc...
I have a form for a user to submit a new password.
what is the best way to replace the line in the file that matches the users submission?
i could read the file into an array, but how do you search an array and replace a key and value with a different one? and then you have to rewrite the entire file using the array, yes?
i think preg_replace would work better but I'm no good with regex - i'm a db guy, this file crap is for the pigeons, but it's what my teacher wants!
what's your idea for the best way to replace the line in the file? any examples?
thanks guys!
this is the script i am using so far and all it does is this:
Warning: preg_replace(): Delimiter must not be alphanumeric or backslash...
$new = $_SESSION['name_to_change'].",".$_POST['new_pass'];
$old = $_SESSION['name_to_change'].",".$_SESSION['old_pass'];
$text = file($filename) or die("unable to read file");
$fh = fopen($filename, 'w') or die("unable to open file for writing");
foreach($text as $line)
{
fwrite($fh, preg_replace($old, $new, $line));
}
the warning come from the fwrite line...
name,password(encrypted with md5)
name,pass...
name,pass..
etc...
I have a form for a user to submit a new password.
what is the best way to replace the line in the file that matches the users submission?
i could read the file into an array, but how do you search an array and replace a key and value with a different one? and then you have to rewrite the entire file using the array, yes?
i think preg_replace would work better but I'm no good with regex - i'm a db guy, this file crap is for the pigeons, but it's what my teacher wants!
what's your idea for the best way to replace the line in the file? any examples?
thanks guys!
this is the script i am using so far and all it does is this:
Warning: preg_replace(): Delimiter must not be alphanumeric or backslash...
$new = $_SESSION['name_to_change'].",".$_POST['new_pass'];
$old = $_SESSION['name_to_change'].",".$_SESSION['old_pass'];
$text = file($filename) or die("unable to read file");
$fh = fopen($filename, 'w') or die("unable to open file for writing");
foreach($text as $line)
{
fwrite($fh, preg_replace($old, $new, $line));
}
the warning come from the fwrite line...