I have it set up to where I log each view in a table called question_views so each user can only be counted once.
However, I have a problem. Right now when you’re not logged in the view count just keeps incrementing because I don’t have a way to identify unregistered people (or people not logged in).
This is how I enter the users into the database for when they view:
function add_view($user, $question) {
$ts = time();
$check = mysql_num_rows(mysql_query("SELECT * FROM question_views WHERE user='$user' AND question_id=$question"));
if($check==0) {
mysql_query("UPDATE questions SET views=views+1 WHERE qid=$question");
mysql_query("INSERT INTO question_views (user,question_id,date) VALUES ($user,$question,$ts)");
}
}
When a user isn’t logged in, $_SESSION['userid'] (which is what is passed when the function is called) doesn’t even exist.
What would be a good way to make a view count system counting each viewer only once, including unregistered people?
EDIT: Of course there isn’t a way to do it reliably. But there is a way to do it at least somewhat good.
Corbin’s got it right. Personally I’d use a cookie though. IP’s can be shared among several people, and sessions only last until you close your browser. I think a cookie’s your best bet, but of course, it can be deleted. Just set the lifetime to ‘forever’ or a long interval.