I'm creating a crude view counter for my site that will update the database and then set a cookie with the ID of the page so another view won't get inserted. Today I realized that very quickly the user can have tons of cookies depending on how many pages they visit on the site. Is there a better way to only insert one view per user or is this cookie method adequate. Thanks.
PHP View Update Code:
public function updateViews ($id) {
$db = Database::getInstance();
//Set Cookie to indicate viewed
setcookie($id, rand(), time()+3600*24*365*10);
//Set required variables
$currentViews = $this->getData($id)->fetch(PDO::FETCH_ASSOC)['views'];
$newViews = $currentViews + 1;
if (isset($_COOKIE[$id])) {
//Do nothing because cookie isset
} else {
//Define Query
$query = "UPDATE coils SET `views` = '".$newViews."' WHERE `uniqueid` = '".$id."'";
//Prepare Query
$prepareQuery = $db->getConnection()->prepare($query);
//Execute Query
$prepareQuery->execute();
}
$currentViews = number_format($currentViews);
//List results to user
if ($currentViews > 1) {
$this->views = $currentViews . " Views";
} else if ($currentViews <= 1) {
$this->views = $currentViews . " View";
}
}