write to file help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rect0r
Forum Newbie
Posts: 6
Joined: Sat Jun 07, 2003 4:51 pm
Contact:

write to file help

Post 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) &#123;
   print "error! please contact the webmaster.";
   exit; 
&#125; 

$stringtowrite=$name.":".$password;



fwrite($fp, $stringtowrite."\n".$stringtowrite2);

fclose($fp);
?>

<?php 
echo "Username: $name Password: $password wrote to file";
?>
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

file_get_contents() and substr_count() to look for $name . ':' . $pass in the string?
rect0r
Forum Newbie
Posts: 6
Joined: Sat Jun 07, 2003 4:51 pm
Contact:

Post by rect0r »

how would I apply those to my script?
I am new to PHP so I am still learning.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
rect0r
Forum Newbie
Posts: 6
Joined: Sat Jun 07, 2003 4:51 pm
Contact:

Post 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.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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

}
?>
Last edited by McGruff on Thu Aug 11, 2005 6:45 am, edited 1 time in total.
rect0r
Forum Newbie
Posts: 6
Joined: Sat Jun 07, 2003 4:51 pm
Contact:

Post by rect0r »

Fatal error: Call to undefined function: file_get_contents()
User avatar
discobean
Forum Commoner
Posts: 49
Joined: Sun May 18, 2003 9:06 pm
Location: Sydney, Australia
Contact:

Post 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
rect0r
Forum Newbie
Posts: 6
Joined: Sat Jun 07, 2003 4:51 pm
Contact:

Post 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) &#123; 

print "This username already exists.";
exit; 

&#125; ELSE &#123; 

    fwrite($fp, $stringtowrite."\n".$stringtowrite2);

fclose($fp); 

echo "Username: $name Password: $password wrote to file";
&#125; 
?>
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
ayron
Forum Newbie
Posts: 14
Joined: Tue Jun 03, 2003 11:18 pm
Location: Perth, Australia

Post by ayron »

where do you open the $fp file?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

ayron wrote:where do you open the $fp file?
Ah yes.. Gosh it's hard to read unhighlighted code :wink:
rect0r
Forum Newbie
Posts: 6
Joined: Sat Jun 07, 2003 4:51 pm
Contact:

Post by rect0r »

not sure where to
ayron
Forum Newbie
Posts: 14
Joined: Tue Jun 03, 2003 11:18 pm
Location: Perth, Australia

Post by ayron »

before you write to it i.e before the fwrite line

$fp = fopen($filename, "a");
Post Reply