Page 1 of 1
Need help using fopen, fwrite etc
Posted: Fri Apr 28, 2006 10:03 am
by spudmclard
Hi
First I apologise, your probably all sick of questions on this subject, ive read tutorials but i can get my head round it.
What im trying to do is have a HTML form that i enter the following
Username Password.
I want this information to be written into a file in the following format
username:password
username1:password1
username2:password2
Also if its possible would i be able to show an updated list of these details as i add them..
Once again sorry if its a dumb easy question.
Thanks
Posted: Fri Apr 28, 2006 10:09 am
by feyd
What have you tried?
Posted: Fri Apr 28, 2006 10:42 am
by spudmclard
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Basically to enter the details ive got
Code: Select all
echo "<form name='input' method='post' action='$_SERVER['PHP_SELF'}'>";
echo "<p><label for='firstname'>Name: </label>";
echo "<input type='text' name='name' id='name' />";
echo "<p><label for='password'>Pass: </label>";
echo "<input type='text' name='pass' id='pass' />";
echo "<p><input type='reset' name='reset' value='reset' />";
echo "<input type='submit' name='submit' value='submit' /></p>";
echo "</form>";
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$pass = $_POST['pass'];
$fp = fopen("test.txt","a");
if(!$fp) {
echo 'Error, the file could not be opened or created.';
exit;
}
fwrite($fp, $name, ":", $pass."n");
fclose($fp);
}
?>
But as it stands even that doesnt work..
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Fri Apr 28, 2006 11:01 am
by themurph
Ahhh to be a noob again.
Actually, I'm pretty much a perma-noob, so I can't say much
Where to start... some of this may just personal preference, but:
1. Your
echo's should be written within <??> tags as they are php commands, not html.
actually, I would ditch the echos alltogether in this case.
2. You need to check for page submission BEFORE echoing the content.
3. your
fwrite is completely fubar. see
http://us2.php.net/fwrite for proper usage.
4. I may be totally wrong, but html tag properties should be enclosed in double quotes, or no quotes
at all... never single quotes.
Give this a shot, it should probably work:
Code: Select all
<?php
if (isset($_POST['submit']))
{
$name = $_POST['name'];
$pass = $_POST['pass'];
$fp = fopen("test.txt",'a');
if(!$fp)
{
echo 'Error, the file could not be opened or created.';
exit;
}
fwrite($fp, "$name:$pass\n");
fclose($fp);
}
?>
<form name="input" method="post" action="<? echo $_SERVER['PHP_SELF'] ?>">
<p><label for="firstname">Name: </label>
<input type="text" name="name" id="name" />
<p><label for="password">Pass: </label>
<input type="text" name="pass" id="pass" />
<p><input type="reset" name="reset" value="reset" />
<input type="submit" name="submit" value="submit" /></p>
</form>
Posted: Fri Apr 28, 2006 11:01 am
by feyd
change all but the first commas in your fwrite call to periods. The "n" you have should probably have a backslash in front of the letter so it will add a new line.
Posted: Fri Apr 28, 2006 11:12 am
by Pineriver
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Code: Select all
<form name='input' method='post' action='<?= $_SERVER['PHP_SELF'] ?>'>
<p><label for='firstname'>Name: </label>
<input type='text' name='name' id='name' />
<p><label for='password'>Pass: </label>
<input type='text' name='pass' id='pass' />
<p><input type='reset' name='reset' value='reset' />
<input type='submit' name='submit' value='submit' /></p>
</form>
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$pass = $_POST['pass'];
$fp = fopen("test.txt","a");
if(!$fp) {
echo 'Error, the file could not be opened or created.';
exit;
}
fwrite($fp, "$name: $pass \n");
fclose($fp);
}
?>
A few pointers,
You do not need to echo out html code, you can just use the PHP starting and ending tags to enclose non PHP code.
$_SERVER['PHP_SELF'}' should be $_SERVER['PHP_SELF'']
A few functions where messed up but corrected
and use
[ C O D E ] [ / C O D E ]
WIth out the spaces for any code that you will post
Akk I see the rest of the guys have posted before me, well hope my post helps some
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Fri Apr 28, 2006 12:22 pm
by waradmin
I find the easiest way to learn how to do this stuff is using two seperate files.
Clearly one file (for example form.php) will have the username/password form:
Code: Select all
<form name="input" method="post" action="add_data.php">
<p><label for="firstname">Name: </label>
<input type="text" name="name" id="name">
<p><label for="password">Pass: </label>
<input type="text" name="pass" id="pass">
<p><input type="reset" name="reset" value="reset">
<input type="submit" name="submit" value="submit"></p>
</form>
Then in file two, have the processing that adds the information to the file:
Code: Select all
<?php
$name = $_POST['name'];
$pass = $_POST['pass'];
$fp = fopen("test.txt",'a');
if(!$fp)
{
echo 'Error, the file could not be opened or created.';
exit;
}
fwrite($fp, "$name".":"."$pass");
fclose($fp);
}
?>
The reason I would do two seperate files is again to make things easier you will know that one file is the form the other adds the data. Once you learn more about PHP add the two files together.
Thats just my advice however. And I do advise agaist storing usernames and passwords in a straight text file, i would recomend storing it in a PHP file that can still be written to but checks to see if someone is trying to directly access the file.
So for example instead of using test.txt name it test.php, it can still be written to, but at the top add something like:
Code: Select all
<?php
echo "Access denied";
exit();
?>
Something like that will work, im 99.99% sure that example wont, im very tired and really just BS'ing this.
But just be sure to change the txt file to a .php file and protect it in a way like I stated so people cant read its contents.
Posted: Fri Apr 28, 2006 2:52 pm
by spudmclard
Thanks everyone for all your help