DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • AI Is Making PHP Cool Again
  • Hadoop on AmpereOne Reference Architecture
  • From 13,000 to 20,000+ Endpoints: Architecting Forensics for the Remote Workforce
  • Bootstrapping a Java File System

Trending

  • Giving AI Agents Safe Access to Legacy Systems: Introducing legacy2mcp
  • Beyond the Endpoint: How Agent Cards Enable Discovery, Interoperability, and Trust
  • Google Cloud KMS vs. AWS KMS: Java Encryption Guide
  • GenAI Security Gateway: An End-to-End Architecture

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. How to Create a Simple PHP Text Counter

How to Create a Simple PHP Text Counter

By 
Michael Bernat
·
Oct. 24, 11 · News
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
16.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in
After learning the basics of PHP's basic file system functions, the first thing you'll want to do is put it to use. One of the easiest and flashiest things you can create is a page counting script. I'll show you how to create a page hit script that is easy to create and even easier to implement.
<?php
  // Part 1
     $filename = "counter.txt";
     $count = file_get_contents($filename);
     if ($count == null)
        $count = 0;
     echo $count;
  // Part 2
     $count++;
     $handle = fopen($filename, "w+");
     fwrite($handle, $count);
     fclose($handle);
?>

This is the entire script. Let's go through it line by line.

$filename = "counter.txt";

This assigns the filename to a variable to be used throughout the rest of the script. In this case I used counter.txt. The first thing to do after implementing this script is to create counter.txt on your server in the same directory as this script and chmod is to 777 so that PHP may write to the file later on. Here is a great tutorial on how to chmod files: http://support.discusware.com/center/resources/howto/chmod.html

 
$count = file_get_contents($filename);

This line stores in entire contents of counter.txt into the variable $count. In this case there is only a number inside counter.txt so $count holds that number. 

 if ($count == null)
       $count = 0;

If the file is empty we need to make a case for that or problems will come up later. If $count does not contain anything than we go ahead and set $count to 0.

 echo $count;

This will display the number inside the text file.  

 $count++;

This adds 1 to the current $count.

$handle = fopen($filename, "w+"); 

The next thing to do is to open the file for writing and to truncate it (erase its contents), that's why I choose w+. Please see my article on file system basics for an explanation on other modes. fopen() requires that we assign a resource handle to be referenced in the future.  

  fwrite($handle, $count);

This line writes the variable $count to our file represented by $handle.  

 fclose($handle);

This is the final line of the script where we do some cleanup. This closes the file we opened earlier. It isn't essential to the script what its always nice to cover all bases.

Now that you understand how the script works it's time to implement it.

 <?php

    include("counter.php");

?> 

 This assumes you name the script above counter.php. Wherever you put this in your page is where the script will echo the page hit count. It's as easy as that. 


PHP File system

Published at DZone with permission of Michael Bernat. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • AI Is Making PHP Cool Again
  • Hadoop on AmpereOne Reference Architecture
  • From 13,000 to 20,000+ Endpoints: Architecting Forensics for the Remote Workforce
  • Bootstrapping a Java File System

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook