使用 PHP 记录页面点击的最佳实践?
我想记录页面的点击次数,并且我正在考虑基于普通文件的存储或 SQLite 存储。
基于文件的选项
文件仅包含一个随每次页面访问而递增的整数,并且每个页面都有唯一的文件名。 如果我使用 a 模式打开文件,我可以写入,但是否可以不关闭它,以便避免每次打开和关闭文件?
SQLite选项
包含 PageName 和 Hits 列的简单表。 再次同样的问题是否可以不关闭它以便避免每次打开和关闭数据库?
I want to record number of hits to the pages and I'm thinking either plain file-based or SQLite storage.
File-based option
A file includes only an integer incremented with each page visit and every page has a unique file name. If I open a file using a mode, I can write but is it possible to not to close it in order to save from opening and closing the file each time?
SQLite option
A simple table with columns PageName and Hits. Same question again is it possible to not to close it in order to save from opening and closing the db each time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您确实必须在内部执行此操作,则应该使用 sqlite 方法。 虽然有一点开销(由于打开数据库文件),但存储结构化数据也有显着的好处。
例如,您可以添加一个日期字段,然后获取每个页面的每日/每小时/每月/任何数据。
您还可以添加每个访问者的 IP 地址,然后提取访问数据。 这样您就可以轻松提取有关网站用户行为的数据。
您还可以存储访问者的用户代理和操作系统,以便您知道应该定位或不定位哪些浏览器。
总而言之,将此类数据插入数据库是微不足道的。 如果您花一些时间研究这些数据,您可以从这些数据中学到很多东西。 因此,数据库通常是最佳选择,因为它们易于操作。
If you really have to do this in-house, you should go with the sqlite method. While there's a little overhead (due to opening the database file), there can also be notable benefits in storing structured data.
You could for example add a date field, and then get daily/hourly/monthly/whatever data for each page.
You could also add the IP address for each visitor, and then extract visits data. That way you could easily extract data about your site users' behaviours.
You could also store your visitors' user-agent and OS, so you know what browsers you should target or not.
All in all, inserting that kind of data into a database is trivial. You can learn a lot of things from these data, if you take some time to study these. For that reason, databases are usually the way to go, since they're easy to manipulate.
Google Analytics。 除非您需要在内部进行。
在 PHP 中推出您自己的解决方案可以像您喜欢的那样简单或复杂。 您可以创建一个表来存储 IP 地址(并不总是可靠)、页面位置和日期。 这将使您能够跟踪每天的独特点击次数。 您可能希望安排一项任务,将记录数量减少到简单的
date
行,numOfUnique
。另一种方法是解析日志文件。 您也可以每 24 小时左右执行一次此操作。
Google Analytics. Unless you need to do it in-house.
Rolling your own solution in PHP can be as simple or complicated as you like. You can create a table to store ip-address (not always reliable), the page location, and a date. This will allow you to track unique hits for each day. You may want to schedule a task to reduce the amount of records to a simple row of
date
,numOfUnique
.Another method is parsing your log files. You could do this every 24 hours or so as well.
在你的任何情况下都是不可能的。 PHP 应用程序在用户向其请求某些内容时运行,它们生成结果然后关闭。 因此,即使您不关闭数据库连接或文件,它们也会自动关闭。 但我不知道为什么打开数据库连接或要写入的文件会出现问题?
It's not possible in any of your cases. PHP applications are run when user requests something from them, they generate the result and then shuts down. So even if you don't close db connection or file they will be closed automatically. But I don't know why opening db connection or a file to write would be a problem?
在没有预期流量的情况下,很难给出特别有用的答案(除了 Jonathan Sampson 的评论,即使用 Google Analytics 可能会更好)。
基于文件的选项:
我认为不可能保持文件打开。 此外,除非您采用某种锁定机制,否则您可能会遇到并发写入问题。
SQLite 选项:
如果您还没有打开数据库,我认为这可能是可行的方法。 我怀疑每次打开/关闭数据库都会成为瓶颈 - 尝试并分析。
It's difficult to give particularly useful answers in the absence of how much traffic you're expecting (other than Jonathan Sampson's comment that you might be better off using Google Analytics).
File-based option:
I don't think it's possible to keep the file open. Also, you'll probably bump into concurrent write problems unless you employ some kind of locking mechanism.
SQLite option:
I think this is probably the way to go, if you've not already got a database open. I doubt that opening/closing the db each time will be a bottleneck - try it and profile.