Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with a few php processes and with filesystem.

Status
Not open for further replies.

SFiend

Programmer
Jun 4, 2004
39
CZ
Hello,
I have a problem with big traffic counter. From time to time filesystem cut the file with data to zero size. Could you help me with it?

Here's the code:
Code:
<?
	error_reporting(0);
	
	define('Data', './count.cnt');

	chdir(dirname(__FILE__));
//php 4
	if (file_exists(Data))
		$File = fopen(Data, 'r+');
	else
		$File = fopen(Data, 'w+');
	
	if ($File !== false) {
		do {
			flock($File, LOCK_EX, $Lock);
//			sleep(1);
		} while ($Lock);
		
		$Content = fread($File, filesize(Data));
		rewind($File);
		$Hosts = unserialize(base64_decode($Content));
			
//php 5 only
//	$Hosts = unserialize((file_get_contents(Data)));
//	$Hosts = unserialize(base64_decode(file_get_contents(Data)));
		
		if (isset($_GET['Id'])) {
			$Hosts[$_GET['Id']][1]++;
	
//php 4
			fwrite($File, base64_encode(serialize($Hosts)));
			flock($File, LOCK_UN);
			fclose($File);
					
//php 5 only
//		file_put_contents(Data, (serialize($Hosts)));
//		file_put_contents(Data, base64_encode(serialize($Hosts)));
			
			header("Location: ".$_GET['Out']);
			exit();
		}
		else {
			$Url = parse_url($_SERVER['HTTP_REFERER']);
			$Host = strtolower(str_replace('[URL unfurl="true"]www.',[/URL] '', $Url['host']));
			
			if ($Host != '') 
				$Hosts[$Host][0]++;
			else
				$Hosts['!unknown'][0]++;
			
//php 4
			fwrite($File, base64_encode(serialize($Hosts)));
			flock($File, LOCK_UN);
			fclose($File);
	
//php 5 only
//		file_put_contents(Data, (serialize($Hosts)));
//		file_put_contents(Data, base64_encode(serialize($Hosts)));
		}
	}
		
?>
 
No error :(

I call this script approximately 200000 during the day. And there is big chance to call this script twice in the same time.

One process open the file, read it and cut to zero size for writing again and I think the second process read the file at the same time when the first one cut it to zero. Then the second one write only new value. :(
 
I notice that you're using flock() on the same file you're trying to write to. The PHP online manual entry on flock() ( has a note that reads:

Note: Because flock() requires a file pointer, you may have to use a special lock file to protect access to a file that you intend to truncate by opening it in write mode (with a "w" or "w+" argument to fopen()).

Although you're not truncating, this may be apropos.

Also, have you tried logging to a database server or using error_log() to do your logging? (I have no idea if error_log() is multi-user safe)


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top