Jump to content

Saving to Text File


MacroHawk

Recommended Posts

How would I make form with 2 text boxes 1 is username and 2 is password and 1 button that when you click it, it will save the username anf password into 2 speract text files. Break down:

A register program.

Writes the username in 1 text (user.txt) and password in another (pass.txt)

If some one could do this for me I would be so greatful!

Link to comment
Share on other sites

I see what Barand is saying, why have two separate text files?

 

Just format your text file like this:

Username || Password

 

Thats all you need. Then you will have the users username and password right there in the same file. Then when you want to check if they have the correct login information, you just have to explode the line by the "||" and check if there is a match.

 

 

 

 

 

 

Link to comment
Share on other sites

I see what Barand is saying, why have two separate text files?

 

Just format your text file like this:

Username || Password

 

Thats all you need. Then you will have the users username and password right there in the same file. Then when you want to check if they have the correct login information, you just have to explode the line by the "||" and check if there is a match.

 

No. There is 2 textboxs 1 for the user name and 1 for the password. When a person clicks submit it will save the contense of the user name field into user.txt and the contense of the password field in pass.txt

Link to comment
Share on other sites

Yes, I understand what you are saying. Why not just save both text box values to the same text file as I explained?

 

If you use two text files, it is going to be hard to figure out which username goes with which password. Plus there is just no need for it when you can get the same thing accomplished with one text file.

 

Here is a tutorial for how to write text to a file:

http://php.about.com/od/advancedphp/ss/file_write_php.htm

 

Link to comment
Share on other sites

Here you are:

 

<?php

if ($_POST['submit']) {
    
    if (isset($_POST['username']) && isset($_POST['password'])) {
        //Write the username to the user.txt file
        $File = "user.txt";
        $Handle = fopen($File, 'a');
        $Data = "{$_POST['username']}\n";
        fwrite($Handle, $Data);
        fclose($Handle);
        
        //write the password to the pass.txt file
        $File = "pass.txt";
        $Handle = fopen($File, 'a');
        $Data = "{$_POST['password']}\n";
        fwrite($Handle, $Data);
        fclose($Handle);
        
        print "Data Written<p>";
        
    } else {
        echo "You left something blank, please fill both fields in.<p>";
    }
    
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Username: <input type="text" name="username"><br>
Password: <input type="text" name="password"><br>
<input type="submit" name="submit" value="Submit">
</form>

 

Remember to save this as a .php file.

Link to comment
Share on other sites

<?php

if ($_POST['submit']) {
    
    if (isset($_POST['username']) && isset($_POST['password'])) {
        
        $username = trim($_POST['username']);
        $pass = trim($_POST['password']);
        
        //Write the username to the user.txt file
        
        $lines = file('user.txt');
        
        foreach($lines as $line){
            $line = trim($line);
            
            if ($line == $username) {
                echo "The username you chose already exists!";
                exit;
            }
        }
        
        $File = "user.txt";
        $Handle = fopen($File, 'a');
        $Data = "$username\n";
        fwrite($Handle, $Data);
        fclose($Handle);
        
        //write the password to the pass.txt file
        $File = "pass.txt";
        $Handle = fopen($File, 'a');
        $Data = "$pass\n";
        fwrite($Handle, $Data);
        fclose($Handle);
        
        print "Data Written<p>";
        
    } else {
        echo "You left something blank, please fill both fields in.<p>";
    }
    
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Username: <input type="text" name="username"><br>
Password: <input type="text" name="password"><br>
<input type="submit" name="submit" value="Submit">
</form>

Link to comment
Share on other sites

Actualy... It sorta does and sorta doesn't... To make it so that people can't view the text I CHMOD it to 662. And when I do that then the check if the user name is all ready there does not work... But when I take off the CHMOD so that every one can view it does work... But I need it so that people can't view?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.