Jump to content

Pageview Counter


Gyohdon

Recommended Posts

Right now I'm working on a pageview counter. The counting itself is done, however, I'm trying to protect it from counting each time you press refresh/F5. I just want it to count for each unique visit, not every.Can someone tell me an easy way to do this?EDIT: Still working on this. I don't exactly know what I should use as my SELECTquery to get the desired result.I want to my database to fetch me the last action from a certain ip, based on the date.So, if a user looked at a certain page 2 days ago and yesterday as well, I want it to give me yesterdays view.So far I've got this:

$curdatetime = date("H:i:s, d.m.Y");		$ip = $_SERVER['REMOTE_ADDR'];				$actioncheck = mysql_query("SELECT * FROM `actionlog` WHERE IP = '$ip' 		AND Action = `View` AND ID = '$postid' AND Category = '$category'");				if (!$actioncheck)			{			die('Error: ' . mysql_error());			}

So, what should I add to my SQL query to get the desired result?EDIT 2: Okay, now I need to calculate the difference between two datetimes.I was just wondering, is there a datetime-difference function in php?

Link to comment
Share on other sites

you could log the ip address and timestamp with each counted hit. if an IP address submits a hit within 24 hours of it's last registered hit, don't count it.

Link to comment
Share on other sites

you could log the ip address and timestamp with each counted hit. if an IP address submits a hit within 24 hours of it's last registered hit, don't count it.
How would this be done?In a database, or something else?Also, the page is heavily affected by GET variables, I don't know if that's too important.
Link to comment
Share on other sites

Either in a database or file (database would be easier). You can get the IP and the timestamp like this:

$ip = $_SERVER['REMOTE_ADDR'];$timestamp = time();

Also, the page is heavily affected by GET variables, I don't know if that's too important.
So you want the hits to be tied to a specific page, including the GET variables?
Link to comment
Share on other sites

Either in a database or file (database would be easier). You can get the IP and the timestamp like this:
$ip = $_SERVER['REMOTE_ADDR'];$timestamp = time();

So you want the hits to be tied to a specific page, including the GET variables?

Eaxctly, but I already know how I'm going to do it. I'll create a table in my database which logs all actions (views and rates), together with the get variables, ip and timestamp. Then, based on all that data I will add one to the view count that's tied to that specific page.
Link to comment
Share on other sites

  • 2 weeks later...
EDIT: Still working on this. I don't exactly know what I should use as my SELECTquery to get the desired result.I want to my database to fetch me the last action from a certain ip, based on the date.So, if a user looked at a certain page 2 days ago and yesterday as well, I want it to give me yesterdays view.So far I've got this:
$curdatetime = date("H:i:s, d.m.Y");		$ip = $_SERVER['REMOTE_ADDR'];				$actioncheck = mysql_query("SELECT * FROM `actionlog` WHERE IP = '$ip' 		AND Action = `View` AND ID = '$postid' AND Category = '$category'");				if (!$actioncheck)			{			die('Error: ' . mysql_error());			}

So, what should I add to my SQL query to get the desired result?

I was referring to this ^^
Link to comment
Share on other sites

You need to use ORDER BY to sort by the date, have it sort descending to get the most recent first, and use LIMIT so that it only returns the most recent result.
mysql_query("SELECT * FROM `tablename` WHERE var = 'value' ORDER BY Date DESC LIMIT 1 ");Is that good?
Link to comment
Share on other sites

EDIT 2: Okay, now I need to calculate the difference between two datetimes.I was just wondering, is there a datetime-difference function in php?

Link to comment
Share on other sites

You can also use something like strtotime or mktime to convert dates to numbers, and then just subtract to get the difference.
If I were to use date("U") and store that seperately, then subtract a later date("U") with the old one, would I get the seconds in difference?Would it return the actual amount of seconds?
Link to comment
Share on other sites

yes. timestamps are literally the number of seconds (or milliseconds) since the date 1/1/1970 (called the epoch).I have found this site helpful when dealing with applications making (heavy) use of timestamps.http://www.epochconverter.com/

Link to comment
Share on other sites

yes. timestamps are literally the number of seconds (or milliseconds) since the date 1/1/1970 (called the epoch).I have found this site helpful when dealing with applications making (heavy) use of timestamps.http://www.epochconverter.com/
So, if $var1 and $var2 would be stored with date("U"), would this work?
$result = $var2 - $var2;if ($result < 100){//Difference between two datetimes is less than 100 seconds}else{//Difference between two datetimes is more than 100 seconds}

Link to comment
Share on other sites

can't hurt to try. to be confident you can always add echo's. i.e.

echo 'var1 is: ' . $var1;echo 'var2 is: ' . $var2;$result = $var2 - $var1;echo 'result is: ' . $result;if ($result < 100){//Difference between two datetimes is less than 100 seconds}else{//Difference between two datetimes is more than 100 seconds}

edit: result should have been $var2 - $var1, (was $result = $var2 - $var2)

Link to comment
Share on other sites

can't hurt to try. to be confident you can always add echo's. i.e.
echo 'var1 is: ' . $var1;echo 'var2 is: ' . $var2;$result = $var2 - $var2;echo 'result is: ' . $result;if ($result < 100){//Difference between two datetimes is less than 100 seconds}else{//Difference between two datetimes is more than 100 seconds}

Wow, this will take some time testing. Anyway, I'll be back with some results.
Link to comment
Share on other sites

Wow, this will take some time testing. Anyway, I'll be back with some results.
Back. It worked. If you would like to test it out yourself I could give you the code I just wrote to test it myself. Anyway, I can use this to calculate the amount of seconds between two datetimes, which makes me very happy.
Link to comment
Share on other sites

if you want, you can post for others to see and learn from. I'm sure my implementation would be similar in concept, but probably end up being written slightly differently. Everyone has their own techniques and conventions, but code examples are great way for others to learn from who may have similar interests in this particular problem, or just to expand upon their general knowledge base. Since it's part of the forum, this thread will be searchable for present/future members.

Link to comment
Share on other sites

if you want, you can post for others to see and learn from. I'm sure my implementation would be similar in concept, but probably end up being written slightly differently. Everyone has their own techniques and conventions, but code examples are great way for others to learn from who may have similar interests in this particular problem, or just to expand upon their general knowledge base. Since it's part of the forum, this thread will be searchable for present/future members.
Exactly.So, without further ado, my little epoch-datetime difference experiment!
<?php//Connect to DB, insert your info$database = "DB";$mysql_servername = "localhost";$mysql_user = "root";$mysql_password = "";$con = mysql_connect($mysql_servername, $mysql_user, $mysql_password);if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db($database, $con);//Connected to DB//Create Main Pageecho '<html><body>	<form action="testdate.php" method="post">	Choose mode: <select name="mode">					<option value="Insert">Insert</option>					<option value="Result">Result</option>				</select>	<input type="submit" value="Mode" /></form>';if (isset($_POST['mode']))	{	if ($_POST['mode'] == 'Insert')		{		$time = date("U");				$mysql = mysql_query("INSERT INTO `Datetime` VALUES ('$time')");				if (!$mysql)			{			die('Error: ' . mysql_error());			}				echo 'Succesfully inserted ' . $time . '<br />';		echo '<a href="testdate.php">Click here to return</a>';		}	elseif ($_POST['mode'] == 'Result')		{		$query = mysql_query("SELECT * FROM `Datetime` ORDER BY Date DESC LIMIT 1");				if (!$query)			{			die('Error: ' . mysql_error());			}				$row = mysql_fetch_array($query);				$var1 = $row['Date'];		$var2 = date("U");				echo '<br />';		echo '<br />';		echo '<br />';				echo 'var1 is: ' . $var1;		echo '<br />';		echo 'var2 is: ' . $var2;		echo '<br />';				echo '<br />';		echo '<br />';		$result = $var2 - $var1;		echo 'result is: ' . $result;		echo '<br />';		echo '<br />';		if ($result < 100)			{			echo 'Difference between two datetimes is less than 100 seconds';			}		else			{			echo 'Difference between two datetimes is more than 100 seconds';			}				echo '<br />';		echo '<br />';				echo 'Succesfully calculated the difference.<br />';		echo '<a href="testdate.php">Click here to return</a>';		}	}//Close everything offmysql_close($con);echo '</body></html>';?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...