Page 1 of 1

write permissions

Posted: Sat Jun 12, 2010 12:11 pm
by ief2
Hi,
At my site I host applications for Mac OS X. Now I was wondering how many people clicked my download link. So I started with a test page. Just 3 files (loga, logb & logc) in a folder and a PHP file which would read the file or increase the number of the file. The HTML contained three links for viewing the log files and three links for increasing there count. Viewing the log count doesn't give me any problems, but, as I already was expecting, increasing the count does

This is my PHP file which shows or (tries to) increase the count contained by the file.

Code: Select all

	function getLogCount($logName) {
		$fullFileName = "txts/" . $logName . ".txt";
		
		// read log
		$fileSize = filesize($fullFileName);
		
		if($fileSize <= 0) {
			return 0;
		}
		
		$openedFile = fopen($fullFileName, "r");
		$count = fread($openedFile, filesize($fullFileName));
		fclose($openedFile);
		$count = (integer)$count;
		
		return $count;
	}
	
	function increaseLogCount($logName) {
		$fullFileName = "txts/" . $logName . ".txt";
		
		// read log
		$count = getLogCount($logName);
		
		$newCount = (integer)$count + 1;
		$openedFile = fopen($fullFileName, "w");
		$success = fwrite($newCountn, $openedFile);
		fclose($openedFile);
		
		echo "Writing Succes? " . $success;
	}
	
	
	// MAIN
	$type = $_GET['type'];
	$logname = $_GET['logname'];
	echo "TYPE: " . $type . "<br /><br />";
	if($type == "print") {
		$count =  getLogCount($logname);
		echo "Count in " . $logname . ": " . $count;
	
	} elseif($type == "increase") {
		increaseLogCount($logname);
	
	} else {
		echo "Unknown Command";
	}

Here I have hosted the test project

I hope somebody can help me,
ief2

Re: write permissions

Posted: Sat Jun 12, 2010 12:56 pm
by PHPHorizons
Do you not have a database? Page counters should be stored in a database because you don't have to worry about atomicity. I.e., two people load a page at the same time. The script loads the counter text file and sees the number of pages loads stored there. They both see the same number. They both increment it one. In the end, the page counter only counted one of those page hits.

With a database, you can run a query like this: update `page_counter` set `count` = `count` + 1
And if two people open a page at the same time, you count both of them.

You can retrieve the page count with a query like this: select `count` from `page_counter`

Simple, cheers

Re: write permissions

Posted: Sat Jun 12, 2010 5:19 pm
by ief2
I guess I'm going to try it with MySQL