Page 1 of 1

to delete a line from a file

Posted: Tue Jul 08, 2008 2:44 am
by vb_123
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi all,
i have a file called datafile.txt and the content of that file is.............
parag=parag
a=a
123456=123456
admin1=admin1
kapil=123456

in this file i am storing user name and password like in parag=parag first parag is user name and second parag is password.............
now i am writing a php code to change/update the password..................for that i have written code(its not completed) in "updatepass.php" ...............

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD><TITLE> Update Password </TITLE></HEAD>
 
 <BODY>
  <form  name="frm" method="post" action="updatepass.php">
    <table width="450"  align="center" cellpadding="0" cellspacing="0" >
        <tr>
            <td>Enter Old Password : </td>
            <td><input type="text" name="oldpwd" size="30"></td>
        </tr>
        
        <tr>
            <td>Enter New Password : </td>
            <td><input type="text" name="newpwd" size="30"></td>
        </tr>
 
        <tr>
            <td>Confirm New Password : </td>
            <td><input type="text" name="confpwd" size="30"></td>
        </tr>
          
        <tr>
            <td><input type="submit" name="sub" value="Submit"></td>
        </tr>   
 
    </table>
</form> 
</body>
</html>
 

Code: Select all

<?php
    
    $oldPwd = $_POST['oldpwd'];
    $newPwd=  $_POST['newpwd'];
    $confPwd= $_POST['confpwd'];
 
$file="datafile.txt";
$read=parse_ini_file($file);
foreach($read as $key=>$value)
{
if($value==$oldPwd)
    {
        if($newPwd==$confPwd)
        {
            $fp = fopen('datafile.ini', "w") or die ("cannot open file");
            if(flock($fp, LOCK_EX)) 
            {
                $value=$newPwd;         
                .........................................
..........................................................................
                                                
        }
 
    }
 
}
 
 
?>


after $value=$newPwd; wat i need to do is to store the new password in "datafile.txt "
(to over the old content with a new content and rest file remain same)....so how can i do this............thanx in advance


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: to delete a line from a file

Posted: Tue Jul 08, 2008 9:52 am
by pickle
Search through the text file line by line until you find the username. Delete that line entirely. Then make a new line at the end of the file with the username & new password.

Your best bet though, is to store this stuff in a database - that's what they're for.

And you should NEVER store a password plaintext, much less in a file that's web accessible like it appears yours is. That is a massive security flaw.

Also, why are you opening the file with parse_ini_file()? file_get_contents() is much more practical