PHP:使用Cookie / ip对特定项目的唯一访问/点击 [英] PHP: Unique visits/hits to specific items using cookies/ip

查看:120
本文介绍了PHP:使用Cookie / ip对特定项目的唯一访问/点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个简单的MySQL表来计算网站中特定页面/项目的每日点击量。



每次使用 id列作为页面/商品ID, date作为访问次数/点击数, hits列作为点击数。

有人访问该页面时,该特定日期的匹配数将更新为+1。



但是,每次刷新或重新访问该页面时,该值都会更新一次并且我需要一种方法来将页面ID保存在Cookie或数据库中的访问者IP中,以仅一次更新匹配。



我从不使用Cookie,所以我



我认为在数据库中保存访问者的IP地址是一项艰巨的任务。



确实浪费了查询和追随者,而且我读到它并不总是能够获得真正的IP,这可能会带来一些问题,某些匹配没有更新。



我还读到最好不要过多使用GLOBAL变量...



有人可以向我解释一下做到这一点的最佳方法以及如何可以吗?



谢谢!

解决方案

如果d宁愿将cookie作为PHP变量来处理,我很可能会使用一个会话。

  session_start ()
$ page = 1; //页面ID
if(isset($ _ SESSION [$ page]))
{
//如果该人刷新了
,请执行一些操作}
else
{
$ _SESSION [$ page] = time();
//记录命中。
}

在$ page变量中获取Page ID,它将保存在一个会话。会话就像一个cookie,只是它保存在服务器端。



基本上,如果存在具有该页面ID的变量,它将什么都不做,否则它将记录点击并设置该变量。



您可以查看本教程以获取有关会话的更多信息: http:/ /www.tizag.com/phpT/phpsessions.php



像Cookie一样,会话只会持续一定的时间。因此,它将在几天后记录一次访问。



您甚至可能想要手动查看上次访问的时间。如果超过一个小时,请记录一个新的热门歌曲,因为它们可能不仅仅是刷新。


I use a simple MySQL table to count day hits for specific pages/items in a website. Using 'id' column for page/item ids, 'date' for the day of the visits/hits, and 'hits' column for the number of hits.

Everytime someone visit the page the value of hits for that specific date is updated to +1.

But, the value is updated one more time each time the page is refreshed or revisited and I need a way to save the page ID in a cookie or the visitor IP in the database to update the hits just one time.

I never worked with cookies so I dont really know how to do it and the limitations of the data than can be stored in a cookie.

I think saving the IP adresses of the visitors in the database is a really waste of queires and bandwitch and I read that its not always possible to get the real IP and this can make some problems and some hits not updated.

I also read that its better not to use GLOBAL variables too much...

Can someone please explain me the best way to do it and how to do it??

Thanks!

解决方案

If you'd rather deal with cookies as PHP variables, something you are very familiar with most likely, I'd use a session.

session_start()
$page = 1; // Page ID
if(isset($_SESSION[$page]))
{
    // Do something if the person refreshed
}
else
{
     $_SESSION[$page] = time();
     // record hit.
}

Get the Page ID in the $page variable, and it will save it in a session. A session is like a cookie, except it is saved on the server side.

Basically, if there is a variable with the ID of that page, it will do nothing, else it records a hit and sets the variable.

You can check this tutorial for more on sessions: http://www.tizag.com/phpT/phpsessions.php

Like cookies, Sessions will only last for a certain amount of time. So it will record a visit after a few days.

You might even want to , manually check to see when the last time visited was. If it is over an hour, record a new hit, because they probably aren't just refreshing.

这篇关于PHP:使用Cookie / ip对特定项目的唯一访问/点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆