Jump to content

Recommended Posts

What is the best way to store "page view count", the page view count will increment by 1 if the users IP address is not already stored.

 

I want to plan for the future in case i start to get a lot of visits, what is your opinion for the best way to do this and why?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/254570-page-view-count/
Share on other sites

Here's a really simple solution:

CREATE TABLE `pageview` (
`user_ip` int(11) NOT NULL,
PRIMARY KEY (`user_ip`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

 

// insert
$query = mysql_query("REPLACE INTO pageview (user_ip) VALUES ('" . ip2long($_SERVER['REMOTE_ADDR']) . "')");

// retrieve
list($pageviews) = mysql_fetch_row(mysql_query("SELECT COUNT(user_ip) FROM pageview"));

echo $pageviews;

Link to comment
https://forums.phpfreaks.com/topic/254570-page-view-count/#findComment-1305400
Share on other sites

Thanks scootstah, that's what I had in mind but I have a little doubt in my mind with this solution. My website will have dynamic pages (like youtube, adding videos).

 

If I have 1000 "pages" and 10,000 users each visit all of these pages (I know these figures are not true, but I'm trying to plan ahead) that will equal 10,000,000 rows. Something tells me that isn't going to be good, but I have no real evidence to back up my thinking. Is this a problem?

Link to comment
https://forums.phpfreaks.com/topic/254570-page-view-count/#findComment-1305402
Share on other sites

No it won't. It will only insert a new row if one doesn't already exist. Read: http://dev.mysql.com/doc/refman/5.0/en/replace.html

 

EDIT: Wait, nevermind...I see what you mean.

 

10,000 rows is really diddly in a database. This forum alone has well over 1 million rows just for posts.

 

EDIT 2: Oh god, I can't read today. 10 million...right.

Link to comment
https://forums.phpfreaks.com/topic/254570-page-view-count/#findComment-1305405
Share on other sites

If you want unique visits (by checking an IP) then unfortunately you have to deal with this. If potentially fake views aren't a problem then you can simply increment the view on every page load. I think most sites use this method, including Youtube.

Link to comment
https://forums.phpfreaks.com/topic/254570-page-view-count/#findComment-1305408
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.