9633

How to get page view data like stackoverflow?

Question:

I want to get and display how many times a page is viewed, just like stackoverflow.

How to do it by php? Thanks!

Answer1:

if (file_exists('count_file.txt')) { $fil = fopen('count_file.txt', r); $dat = fread($fil, filesize('count_file.txt')); echo $dat+1; fclose($fil); $fil = fopen('count_file.txt', w); fwrite($fil, $dat+1); } else { $fil = fopen('count_file.txt', w); fwrite($fil, 1); echo '1'; fclose($fil); } ?>

For any "decent" counter I would recommend to use a database (mysql, redis ) and trace IP address to have even deeper analytics (e.g how many unique visits, where they are comming from etc)

Answer2:

You will need to store that information somewhere, which isn't really something that you can do with <em>only</em> PHP.

Most commonly, this is stored in a database. The simplest solution is a single database row per page that you wish to track with a column for remembering the view count. You would increment this column each time the page is loaded.

Slightly more complicated, but far more useful, would be to add a database row on each page load, noting the page, time, and any other information that you might find useful.

Another simple way to capture this information would be to install an analytics package on your site. Something like <a href="http://www.google.com/analytics" rel="nofollow">Google Analytics</a>, which is free. But, it isn't particularly tailored to displaying page views, simply capturing them.

Recommend

  • Explanation of working principle of openGL [closed]
  • Can't have several threads per session
  • Swift convert Data to UnsafeMutablePointer
  • How to convert get perticular field from PHP array to an array in Javascript? [closed]
  • How to call a postgresql function from the JOOQ models in a select statement?
  • image view zoom with finger spread in android like gallery images
  • where is memory allocated for pointers and their data?
  • Checking metadata of Amazon S3 file using iOS AWS SDK in Swift
  • Extract text from “” HTML tag
  • Setting up ElastiCache Redis with Elastic BeanStalk + Django
  • docker-compose: connection refused between containers, but service accessible from host
  • Google OAuth2 for an web application hosted behind NAT (intranet server without public IP)
  • How to best manage SMTP clients
  • unrecognized selector isPitched called
  • get all processes in parallel
  • How to resolve docker host names (/etc/hosts) in containers
  • Swift iOS don't recognize GCM library imports?
  • maven jboss-as:start A required class was missing … org/sonaty…/ArtifactResolutionException
  • Should I be afraid to use UDP to make a client/server broadcast talk?
  • MYSQ & MVC3 SQL connection error \\ ProviderManifestToken but I am using MySQL
  • Graphics.CopyFromScreen [Web application] + The handle is invalid
  • How VBA declared Volatility works
  • Declaring variable dynamically in VB.net
  • Transactional Create with Validation in ServiceStack Redis Client
  • Problem deserializing objects from cache on MyBatis 3/Java
  • Moving mysql files across servers
  • Cannot resolve symbol 'MyApi'
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • Recording logins for password protected directories
  • Exchange data b/w iOS devices using Bluetooth 4.0
  • How to extract text from Word files using C#?
  • How to check if every primary key value is being referenced as foreign key in another table
  • Sending data from AppleScript to FileMaker records
  • How to handle AllServersUnavailable Exception
  • Comma separated Values
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?