Page 1 of 1
write to file help
Posted: Sat Jun 07, 2003 4:51 pm
by rect0r
Hello.
I am new to php and am trying to setup a script that will
allow people to register to a plain text file in this format user:password.
So far I have gotten the file to write the data but I would like
for the script to compare the data already in the file with the
data a user is trying to write to that file so that if the data they
are trying to write already exist it will tell them so and not write
the data again.
Code: Select all
<?php
$name = "";
$password = "";
$fp = fopen("registered","a");
$fp = fopen("registered","a");
if(!$fp) {
print "error! please contact the webmaster.";
exit;
}
$stringtowrite=$name.":".$password;
fwrite($fp, $stringtowrite."\n".$stringtowrite2);
fclose($fp);
?>
<?php
echo "Username: $name Password: $password wrote to file";
?>
Posted: Sat Jun 07, 2003 5:29 pm
by McGruff
file_get_contents() and substr_count() to look for $name . ':' . $pass in the string?
Posted: Sat Jun 07, 2003 6:00 pm
by rect0r
how would I apply those to my script?
I am new to PHP so I am still learning.
Posted: Sat Jun 07, 2003 6:51 pm
by McGruff
Do you have the php manual? Can download a copy from php.net - get the one with user comments.
If you have read up about it and are still stuck I'll write it out.
Posted: Sat Jun 07, 2003 10:23 pm
by rect0r
Ok i've been reading over
http://us3.php.net/substr_count
and the other but I can't seem to grasp howto fit this into my script.
Maybe I am just looking from the wrong end of the book.
Posted: Sat Jun 07, 2003 11:27 pm
by McGruff
Code: Select all
<?php
$file = file_get_contents($filename);
$search_string = $name . ':' . $pass;
IF (substr_count($file, $search_string) > 0) {
// name & pass found
} ELSE {
// name & pass not found
}
?>
Posted: Sun Jun 08, 2003 1:21 pm
by rect0r
Fatal error: Call to undefined function: file_get_contents()
Posted: Sun Jun 08, 2003 1:31 pm
by discobean
Depending on what version you have, file_get_contents() might not work, try:
Code: Select all
<?php
// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen ($filename, "r");
$contents = fread ($handle, filesize ($filename));
fclose ($handle);
?>
The contents of the file will be stored in the $contents variable
Refer to:
http://www.php.net/manual/en/function.f ... ntents.php
http://www.php.net/manual/en/function.fread.php
Posted: Sun Jun 08, 2003 2:55 pm
by rect0r
That fixed that problem and lead me to another one.
Warning: Supplied argument is not a valid File-Handle resource in /host/p/h/p/8/b/i/ on line 22
Warning: Supplied argument is not a valid File-Handle resource in /host/p/h/p/8/b/i/ on line 24
do I need to change the variable? or the entire else statement.
Code: Select all
<?php
$name = "test";
$password = "test99";
$stringtowrite=$name.":".$password;
$filename = "registered";
$handle = fopen ($filename, "r");
$contents = fread ($handle, filesize ($filename));
fclose ($handle);
$search_string = $name . ':' . $password;
IF (substr_count($file, $search_string) > 0) {
print "This username already exists.";
exit;
} ELSE {
fwrite($fp, $stringtowrite."\n".$stringtowrite2);
fclose($fp);
echo "Username: $name Password: $password wrote to file";
}
?>
Posted: Sun Jun 08, 2003 7:44 pm
by McGruff
What's the owner/group of the folder?
You may need to chown - if can't try creating the folder with php mkdir.
Use an FTP program to examine permissions.
Posted: Sun Jun 08, 2003 9:51 pm
by ayron
where do you open the $fp file?
Posted: Mon Jun 09, 2003 12:16 am
by McGruff
ayron wrote:where do you open the $fp file?
Ah yes.. Gosh it's hard to read unhighlighted code

Posted: Mon Jun 09, 2003 10:26 pm
by rect0r
not sure where to
Posted: Mon Jun 09, 2003 10:40 pm
by ayron
before you write to it i.e before the fwrite line
$fp = fopen($filename, "a");