Submit Resources  Users' Login
img

Learn how to make a text counter in PHP.
Home » Tutorials » Php »



Author: Akash Goel
Added: August 13, 2006

A counter is an essential part of a site to know how many people are coming to site. Here is an easy way to make a counter. All you need is access to PHP and be able to chmod a directory. We're going to save the amount of hits in a .dat file, so you don't even need a mySQL database. First off, you have create a directory and CHMOD it 777 so PHP can write the counter.dat file.

Here is what we have to write in English:
  1. Use the file_exists() function in PHP and see if the counter.dat file exists. If it does, then open the file using the PHP fopen() function.
  2. Use the PHP fgets() function to find the number of hits there and save it in a variable.
  3. Add one to the variable.
  4. Close the file.
  5. Display the number of hits.
  6. Open the counter.dat file using the fopen() function and save it as a variable.
  7. Use the PHP fputs() function to insert the new number of hits.
  8. Close the file.
  9. If the counter.dat file doesn't exist, then use the fopen() function and declare it as a variable.
  10. Use the Fputs() function and put a "1" in there.
  11. Print out that 1 person has come to the page and then close the file.
Here it is in PHP:
<?if(file_exists("counter.dat")){  $exist_file = fopen("counter.dat", "r");  $new_count = fgets($exist_file, 255);  $new_count++;  fclose($exist_file);  print("$new_count people have visited this page");  $exist_count = fopen("counter.dat", "w");  fputs($exist_count, $new_count);  fclose($exist_count);}else{$new_file = fopen("counter.dat", "w");fputs($new_file, "1");print("1 person have visited this page");fclose($new_file);}?>
Here is some useful information about these functions, which may be helpful for you that I got from the PHP Manual:

  1. int fopen (string filename, string mode, int use_include_path]) Filename: the name of the file Mode:

      'r' - Open for reading only; place the file pointer at the beginning of the file.
      'r+' - Open for reading and writing; place the file pointer at the beginning of the file.
      'w' - Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
      'w+' - Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
      'a' - Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
      'a+' - Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
    use_include_path:
      Set it to "1", if you want to search for the file in the include_path.
  2. string fgets (int fp, int length) - Gets line from file pointer
      Returns a string of up to length - 1 bytes read from the file pointed to by fp. Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first).


  3. int fputs (int fp, string str [, int length])
      fwrite() writes the contents of string to the file stream pointed to by fp. If the length argument is given, writing will stop after length bytes have been written or the end of string is reached, whichever comes first.


Written by: Akash Goel

Trusted Websites
Create Htm.com - offers Html t...


 
                                                                                   -- Site Pro News      ComputerScripts.com - Free & Commercial Web Scripts!      Ex-design.net