comparing lines

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
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

comparing lines

Post by hob_goblin »

well on my quest to figure out how to make it so someone can not refresh and re-send the same information, i thought it would be best to check the latest entry of a file, and if it is the same as the submitted form, don't add it...

here is what i came up with, for some reason it doesnt work..

Code: Select all

$talkdatafile = "tc.txt";
if ($action == "talk") {
 if(strlen($nick) >= 3 OR strlen($nick) <= 16)&#123;
  if(strlen($stuffsaid) >= 3 OR strlen($stuffsaid) <= 60)&#123;
   if($nick != "Name")&#123;
    if($stuffsaid != "Message")&#123;
	$t_fp = fopen ($talkdatafile, "a+");
	$ip = getenv(REMOTE_ADDR);
	$nicktwo = ereg_replace('<', ' ', $nick);
	$nickthree = ereg_replace('>', ' ', $nicktwo);
	$one_stuffsaid = ereg_replace('<', ' ', $stuffsaid);
	$two_stuffsaid = ereg_replace('>', ' ', $one_stuffsaid);
	$snickthree = stripslashes($nickthree);
	$stwo_stuffsaid = stripslashes($two_stuffsaid);
	$writedata = $snickthree."|".$stwo_stuffsaid."|".$ip."|";
	$test_last = file($talkdatafile);
	$test_lastr = array_reverse($test_last);
	$last_e = $test_lastr&#1111;0];
       if($writedata != $last_e)&#123;
	fwrite ($t_fp, $writedata."\n");
	fclose ($t_fp);
&#125;
&#125;
&#125;
&#125;
&#125;
&#125;
for some reason it always writes the content... any ideas?
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

Post by lc »

Lol I had exactly the same problem the first time I tried it.

The problem is you add \n only in the write function.

$last_e includes the \n on that line from the textfile.

$writedata = $snickthree."|".$stwo_stuffsaid."|".$ip."|";

should be
$writedata = $snickthree."|".$stwo_stuffsaid."|".$ip."|.\n";

and
fwrite ($t_fp, $writedata."\n");
should be
fwrite ($t_fp, $writedata);

that should do it.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

awesome. thanks, it works.
Post Reply