I have read a COOKIE representative for every article. Is it reasonable? One of the features of this article is to count the number of views (traffic) of the current article. my article address is & nbsp; news. php? Id]; if (& nbsp ;! Isset ($ _ COOKIE [user]) & nbsp?
One of the features of this article is to count the number of views (traffic) of the current article)
My article address is news. php? Id = 123
Find a method on the Internet
$gid = $_GET["id];
if( !isset($_COOKIE["user"]) ){
setcookie("user","newGuest",time()+3600);
}else {
setcookie("user","oldGuest");
}
if ( $_COOKIE["user"] != "oldGuest") {
$hits_now = $row["hits"]+1;
$pdo->exec("UPDATE `gamedb_game` SET `hits`='$hits_now' WHERE `id` = '$gid'");
}
In this way, the second refresh of the same browser will not repeat the viewing count.
But I thought it was wrong. I went to another article and tried it. even if I browsed it for the first time, I did not view it any more.
What do I think is better?
$gid = $_GET["id];
if( !isset($_COOKIE["user"]) ){
setcookie("user","newGuest",time()+3600);
}else {
setcookie("user","oldGuest_$gid");
}
if ( $_COOKIE["user"] != "oldGuest_$gid") {
$hits_now = $row["hits"]+1;
$pdo->exec("UPDATE `gamedb_game` SET `hits`='$hits_now' WHERE `id` = '$gid'");
}
Change oldGuest to oldGuest _ $ gid. after adding an article id to the end, will it be unique?
But to some extent, will this cause adverse reactions to users' browsers?
Or does other cms do this?
If you want to write an SQL statement to match whether it is the first time, it is too complicated.
Only cookie
What if this method is applied to my own host and to users?
------ Solution --------------------
It's okay to refresh the number and add it once,
If you really want to use your method to make it invisible, you can put it in the cache, set a time to take it once, many websites do this.
In your method, you can try to put the article id in an array, and then repeat it in sequence and store it in the cookie trial.
------ Solution --------------------
Record the title of the document viewed by the user to the cookie. The next time the user opens the webpage to view the article, there will be a record of the previous view,
This is a bit like the last browsing record of your shopping website when you browse products.