read file for names if there die if not write, 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
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

read file for names if there die if not write, help???

Post by fresh »

hi,
trying to write a script that opens a txt file and reads it:

if you were to read this file, it would look like this:

johnny
suzi
kelly
Blare
Tony
etc...

that way the script can read each line one at a time... my problem is actually writing something that can do that, and tell the script if it found it, and die if so, else it would write there name in the list, enumerating that info, via session var...

my guess:

Code: Select all

$df = fopen("blah.txt", "a+");
$xf = file_get_contents($df);
if(!xf) {
@fwrite($df,$_SESSION['usernam'] "\n");
@fclose($df);
} else {
echo "You have already done this once!";
@fclose($df);
}
thanks ;)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

<?php

$names = file("blah.txt");
if(array_search($_SESSION['usernam'],$names) !== false)
  echo "you done this";
else
{
  $fp = fopen("blah.txt","ab");
  fputs($fp,$_SESSION['usernam']);
  fclose($fp);
  echo "you added";
}

?>
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

great

Post by fresh »

thanks again feyd, it works prety good, I appreciate your help, like always :)
Post Reply