关于在 PHP 中创建简单页面计数器的两部分问题

发布于 2024-11-16 21:35:12 字数 732 浏览 3 评论 0原文

1) 如何向 PHP 页面添加简单的页面计数器并将值插入 MySQL 表中?另外,我需要在每次新访问时更新 MySQL 表值。

2) 窍门在于,PHP 页面是各种用户生成的登陆页面的模板。例如,我希望每个页面都有自己单独的计数器:

examplesite.com/template.php?getvalue=bob

examplesite.com/template.php?getvalue=sam

examplesite.com/template.php?getvalue=samantha

我的印象是,如果我将计数器放在“template.php”文件中,那么它将把每个用户的所有访问量累加起来。我想要的输出是让每个用户只获得单个登陆页面的计数。

因此,如果总共有 12 次访问,分布如下:

examplesite.com/template.php?getvalue=bob  had 4 visits

examplesite.com/template.php?getvalue=sam  had 2 visits

examplesite.com/template.php?getvalue=samantha  had 6 visits

那么我希望 bob 的页计数器读为“4”,sam 的读为“2”,samantha 的读为“6”。我是否正确地假设,如果我将计数器放在 template.php 上,每个用户的登陆页面都会读取为“12”?您有解决此问题的简单方法吗?

1) How do I add a simple page counter to a PHP page and insert the values in a MySQL table? Also I would need the MySQL table value to be updated with each new visit.

2) The trick is that the PHP page is a template for a variety of user generated landing pages. For example, I would like each of these pages to have their own separate counters:

examplesite.com/template.php?getvalue=bob

examplesite.com/template.php?getvalue=sam

examplesite.com/template.php?getvalue=samantha

My impression is that if I put the counter on the "template.php" file then it will add up all the visits from each user to a grand total. The output that I would like is to have each user only get counts for the individual landing page.

So, if there are a total of 12 visits, dispersed as follows:

examplesite.com/template.php?getvalue=bob  had 4 visits

examplesite.com/template.php?getvalue=sam  had 2 visits

examplesite.com/template.php?getvalue=samantha  had 6 visits

then I would want bob's page counter to read as '4', sam's as '2' and samantha's as '6.' Am I correct in assuming that if I just put the counter on template.php that each user's landing page would read as '12?' Do you have a solution for an easy way to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别挽留 2024-11-23 21:35:12

这很简单:

pdo::prepare( 'UPDATE counter SET hits = hits+1 WHERE value = ?');
pdo::execute($_GET['getvalue']);

if ( pdo::rowCount() == 0 ) {
  pdo::prepare('INSERT INTO counter (?,0)');
  pdo::execute($_GET['getvalue']);
}

That's pretty simple:

pdo::prepare( 'UPDATE counter SET hits = hits+1 WHERE value = ?');
pdo::execute($_GET['getvalue']);

if ( pdo::rowCount() == 0 ) {
  pdo::prepare('INSERT INTO counter (?,0)');
  pdo::execute($_GET['getvalue']);
}
我纯我任性 2024-11-23 21:35:12

好吧,正如您可以在生成页面时分离对 bob 和 sam 的请求一样,您可以对计数器执行相同的操作吗?

您可能会使用 $_GET['getvalue'] 执行某些操作。只需在查询中使用该值(转义的、参数化的等)来更新您的计数器...

 UPDATE yourtable SET count = count + 1 WHERE pagename = ?

然后绑定 getvalue...

Well, just as you are able to separate the requests for bob and sam when generating the page, you can do the same for the counter?

You'll probably do something with $_GET['getvalue']. Just use that value (escaped, paramterized etc) in a query to update your counter....

 UPDATE yourtable SET count = count + 1 WHERE pagename = ?

and then bind the getvalue...

掌心的温暖 2024-11-23 21:35:12

您希望在 MySQL 表中为每个用户保留一行。当您要更新表时,通过读取 getvalue 来更新该用户的相应行。然后执行相同的操作来显示用户的页面计数器。

You would want to have a row in your MySQL table for each user. When you go to update the table, update the appropriate row for that user by reading the getvalue. Then do the same for displaying the user's page counter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文