Jump to content

How to record the number of views to a page?


corrupshun

Recommended Posts

I want to do it like a forum does, where everytime the page loads i could add 1 to the views in the database, while also making sure that the same IP doesn't add more than once? I know i could select the database then count it, then add it, but is there a simplier way (while implementing the same IP concept?)

A link would be suffice :)

-Aus THE BOSS

if your pages are generated from content in the db then you will have a db record for each page.

 

This content info in the db needs to have a views field

 

then whenever that page is loaded you include a query to update the views by iincrementing by 1

 

 

if your pages are generated from content in the db then you will have a db record for each page.

 

This content info in the db needs to have a views field

 

then whenever that page is loaded you include a query to update the views by iincrementing by 1

 

I know that, I'm mostly asking how to check if the same IP has been to the page before, and if so, to not increment it.

Ok yes, missed the IP bit.

 

In that case, you would need a separate table that had fields for the page id and the IP.

 

Then the increment function would need to check there to decide to increment or not.

Also, the increment function could do the add to that table if not arleady there

You would need to grab the clients IP address and store it in a table.. Then when a person visits the site you can check to see if that IP address is in the table or not.. If the address is in the table do nothing if it is not in the table add it.. then to get the total visitors you simply count() the number of rows in that table..

Something like this..

// Do the checking and inserting //
$sql = "SELECT ip_address from visitors WHERE ip_address = $usersIP";
$sql_query = mysql_query($sql) or trigger_error(mysql_error());
if (mysql_num_rows($sql_query) == 0) {
mysql_query("INSERT INTO visitors SET ip_address = $usersIP");
}

// Counting
$sql = "SELECT COUNT(ip_address) as `count` FROM visitors";
$sql_query = mysql_query($sql) or trigger_error(mysql_error());
$data = mysql_fetch_assoc($sql_query);
echo 'Number of Unique Visitors: ' . $data['count'];
?>

 

Or something like that. Could be better but its a start for you.

 

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.