Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

AJAX / PHP Count Up Script

AJAX ajax

  • Please log in to reply
1 reply to this topic

#1 Howdy_McGee

Howdy_McGee

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 102 posts

Posted 06 January 2012 - 08:14 AM

I am trying to duplicate this: USAgain <--- On the left of their page they have a counter. I've literally tried everything I can think of but they have something going on in their PHP that I do not. My code is fairly straightforward:

HTML/CSS/Javascript/AJAX
<html>
<head>
<title>Counter</title>
<script language="javascript" src="../jquery1.6.js"></script>
<script type="text/javascript">
	
	function addCommas(nStr) //http://www.mredkj.com/javascript/nfbasic.html -- Source
	{
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	}
	
	function getNum()
	{
		$.post('test.php', function(data){
			$('#counter').html(addCommas(data));
		})
	}	
	setInterval(getNum, 1000);
</script>
<style type="text/css">
	#counterContainer{color: #52504D;font-family:Verdana, Geneva, sans-serif;font-weight:bold;font-size:15px;position:relative;top:22px;}
	#counter{color: #1E7EC8; font-size: 25px;letter-spacing:1px;}
</style>
</head>
<body onload="getNum()">
	<div id="counterContainer">
		<div id="counter"><!--Counter Goes Here, Do Not Disturb--></div>
	</div>
</body>
</html>

And my PHP just reads the number in the file, updates the number, ehcos it back to AJAX, and updates the file. Here's the code:
<?php
	$fp = fopen("staticNum.txt", "r+");
	flock($fp, LOCK_EX);
	$num = fgets($fp, 11);
	$num = intval($num)+1;
	
	echo $num; 
	
	fseek($fp, 0, SEEK_SET);
	fputs($fp, "$num");
	flock($fp, LOCK_UN);
	fclose($fp);
?>

The problem is if 2 people view the page at the same time, the number will increment by 2 instead of 1. If 3 people view it the number will increment by 3 and so on. How do I prevent this and keep it incrementing by 1 no matter what?
  • 0

#2 jerrywickey

jerrywickey

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 51 posts

Posted 13 January 2012 - 05:34 AM

If you don't want the count to increment when more than one view the page simultaneously try a debounce.


<?php
	$debounce= 2;  // seconds
	$fp= fopen('staticNum.txt', 'r+');
	$num= fgets($fp);
	$num= intval($num);	
	$lasttime= fgets($fp);
	$lasttime= intval($lasttime);
	if (time()>($lasttime+$debounce)){
    	    $num++;
	}	
	echo $num; 
	fseek($fp, 0);
	fwrite($fp, ($num."\n"));
	fwrite($fp, (time()."\n"));
	fclose($fp);
?>

This will prevent incrementing for page views that follow within the debounce time. If a series of people view the page, each less than the debounce time after the last, the page number won't increment. If you want it to increment regardless after the debounce time then.


<?php
	$debounce= 2;  // seconds
	$fp= fopen('staticNum.txt', 'r+');
	$num= fgets($fp);
	$num= intval($num);	
	$lasttime= fgets($fp);
	$lasttime= intval($lasttime);
	if (time()>($lasttime+$debounce)){
    	    $num++;
    	    $lasttime= time();
	}	
	echo $num; 
	fseek($fp, 0);
	fwrite($fp, ($num."\n"));
	fwrite($fp, ($lasttime."\n"));
	fclose($fp);
?>

Hope that helps.

Jerry
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download