I have checked the COOKIE representative added to each article. Is it reasonable?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.