Determine how many times webpage has been accessed [closed]

时光总嘲笑我的痴心妄想 提交于 2019-12-13 07:56:23

问题


I was wondering if there is a way to determine how many times a specific webpage has been accessed on the server. If possible, I would like to do this via cPanel, since the website is made in Wordpress, but the page in question I made from scratch and just uploaded it in another directory. So, I can't rely on plugins (right?) and I don't know enough PHP or JavaScript to communicate with web server and store that info on it...


回答1:


Your server is already doing that, it is called the log file. If you grep it for the given page and count the number of log enteries, you will have the number of vistors who viewed that page. This, however, is inefficient and doesn't "work with CPanel".

Next, you could write a simple script to insert an entry to a database every time someone visits that page. Then your answer could be queried by custom PHP you add to CPanel. This is more effiecient, but would be reinventing the wheel.

But, the real answer is get analytics software. There are a number of products that can do this for you:

  • Google Analytics - This is a Google Product that collects lots of different types of user information, generates powerful reports, and is freely available online. It has the downside of giving that data to google - so if you have a privacy policy, you'd want to specifically call out the Google Analytics tracking you're doing.

  • Piwik - This you can either get cloud-hosted, or host it yourself. It's free open-source software that does most of what Google Analytics does. You'd own your data and that data would reside on your server, there are widgets that could be embedded on pages so with some work could be integrated into CPanel. The reports are not as polished looking as Google Analytics, but that might be worth the trade-off.




回答2:


If you use the include function in PHP you can make a statistics PHP file and include it on all your pages.

The easiest way would be to simply create a log file.

Using file_put_contents you can easily append data to a log file, that you can then either download or access through your FTP of choice. (Like cpanel)

<?php
session_start();
if(!isset($_SESSION['mySiteWasAccessed'])) {
    $_SESSION['mySiteWasAccessed'] = true;
    file_put_contents("my_log_file.txt", "My page was accessed", FILE_APPEND | LOCK_EX);
}
?>

The code above will write a new line to a .TXT file the first time a unique user accesses the script.

Now you just need to include a .PHP file containing this information on each page of your site.

See the following links for documentation, pointers, examples and general help:

http://php.net/manual/en/function.file-put-contents.php

http://php.net/manual/en/function.include.php

If you want to get advanced you can look at the mail function for a notification method:

http://php.net/manual/en/function.mail.php



来源:https://stackoverflow.com/questions/35273358/determine-how-many-times-webpage-has-been-accessed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!