Page 1 of 1

Help with fopen() & file() ?

Posted: Thu Aug 29, 2002 12:04 am
by rxsid
Hi all,
I'm trying to creat a text login file. Problem is that sometimes the previously written user|time is not retained, and sometime the time is truncated and place on a newline as a new entry.
This code should just open the file, build the current array based on appropriate amount of lapsed time, and then put the current user in the file along with the array.

$tnow=date("Gi");
$in=file("TestingIN.txt");

$flag=0;
$cnt = count($in);
for ($x=0;$x < $cnt;$x++) {
list($u_in,$time_in)=split("\|",$in[$x]);

if ("$u" != "$u_in") { //If they dont compare.
if ($time_in>$tnow) {
$tnow1=$tnow+240; //Compensate for different day. Midnight is 000!!
if (($tnow1-$time_in) < 5) { //KEEP if less 5 min.
$old_in[]=$in[$x];
$flag++;
} else {
//Do Nothing
}
} elseif ($time_in < $tnow) {
if (($tnow - $time_in) < 5 ) {
$old_in[]=$in[$x];
$flag++;
} else {
//Do Nothing
}
} else {
//Do Nothing
}
}//END if they dont compare.
}

$newinfo="$u|$tnow\n";

$in2=fopen("TestingIN.txt","w");

$fp=fputs($in2,$newinfo, 40);

for($y=0;$y < $flag;$y++) {
$fp=fputs($in2, $old_in[$y], 40);
}
fclose($in2);

Any ideas why this won't work as stated?

Thanks in advance!

Posted: Thu Aug 29, 2002 4:00 am
by mikeq
$in2=fopen("TestingIN.txt","w");

Using the 'w' parameter completely re-writes the file, removing all previous lines. Look up fopen in the manual for other settings, did you know the manual can be really useful sometimes?