Help me debug a PHP script

1 replies
I'm working on a short script to write some information to a text file.

First of all - here's the script.

Code:
$time = time();
	$ip = $_SERVER['REMOTE_ADDR'];
	$refer = $_SERVER['HTTP_REFERER'];
	if ($refer == "") { $refer = "No Referrer available."; }
	$write = "||$refer^$ip^$time";
	$filename = "stats.txt";

	if($flag == false) { // if the file doesn't exist
		$handle = fopen($filename, 'w') or die("can't open file");
		fwrite($handle, $write);
		fclose($handle);
	} else { //if the file already exists
		//write to file
		$handle = fopen($filename, 'a') or die("can't open file");
		fwrite($handle, $write);
		fclose($handle);
	}
Now, when the script runs, it writes to the file twice and I'm not sure why. Here's a quick faux script.

Gather information -> Does the file already exist? -> (No) Create empty file (Yes) Append gathered information to the end of the file

Can anyone see where this is going wonky? I'm not seeing it here and have a feeling the script is being called twice somehow.
#debug #php #script

Trending Topics