I want to create a create account page for my simple login site where the user clicks a create account button and they are brought to a page with the following form to enter a login name and a password.
<form action = "createaccount.php" method="get">
<h1> Please enter your information to create a new login account</h1>
<p>
<label>Login Name:</label><input type = "text" name = "name" />
<label>Password:</label><input type = "password" name = "pwd" />
<br/><br/>
</p>
<input type = "submit" id = "submit" value = "submit"/>
<input type = "reset" id = "reset" value = "reset"/>
</form>
After the user enters there data into the input boxes I want to run a php script to store this data into a text file called accounts.php (I know it is not secure but this data has no value to me as i am making it up as part of the learning process).
So far I have the following php code to store the data in the file createaccount.php
<?php
$username = $_GET['name'];
$password = $_GET['pwd'];
$filename = 'accounts.txt';
$fp = fopen($filename, 'a+');
fwrite ($fp, $username . "," . $password . "\n");
$fclose ($fp);
echo ("account created");
header("Location: "login.html");
die();
?>
This code I believe should take the inputs from login name and password and store them in a file called accounts.txt in the following format
username1:password1
username2:password2
etc.
then echo the screen account created and then take the user to my login.html page so they can log in with there new account info.
But I try and run the code and it does not save my data to the file at all and when i submit the form it does not direct me back to the my login screen i just get a message saying page cannot be displayed.
I think first you have to check the return value of fopen:
And the same for fwrite...
How to create a simple Login form.
html (login.html)
php (login.php)
Hopefully that helps, any other questions add me on skype. Skype: YouRGenetics Website: ItzGenetics.com
~NOTE If your using a hosting company (GoDaddy,ect) that uses permissions make sure you give all permissions to the php file and the txt file.
There are a few things wrong with your code
$fclose
remove the$
sign. Otherwise error reporting will throw:Then, you have an extra quote in
which should read as:
However, you're doing an echo. You can't echo and have a header. You're outputting before header.
Use echo or header.
Sidenote: You're storing information with GET. At the very least, use POST. You're transmitting this LIVE over the Web and the information will be shown in your Web browser's address bar. Should it ever go LIVE; be careful.
As you said, it's not secure. Use
.htaccess
to protect this file.Example code in
.htaccess
You should also look into the following for password storage:
CRYPT_BLOWFISH or PHP 5.5's
password_hash()
function.For PHP < 5.5 use the
password_hash() compatibility pack
.Add error reporting to the top of your file(s) which will help find errors.
Sidenote: Error reporting should only be done in staging, and never production.
If you want to save it with a colon as a seperator, then change
to
Using text files is a lot of work and demands more resources when working with these, especially when it comes to editing, deleting etc..
The use of a database is by far less maintenance and more secure when using prepared statements.