A A A

Tips: Make a basic PHP hit counter text

Tips: Make a basic PHP hit counter text

A counter is an essential part of a site to know how many people are coming to site. This is a basic site hit counter and an very 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 need to create a new .php file and copy/paste this code into it, don’t forget to make a file named and save it (mine is counter.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);
}
?>

Next, we have to make a part of counter.php, named counter.dat with text editor. we’re going to save the amount of hits in a counter.dat file, so we don’t even need a mySQL database.

Now you can place two files counter.php and counter.dat in difference directory and make sure, CHMOD counter.dat to 777 so that the counter.php can write the counter.dat file (I put mine in root directory).

To integrate into your WP theme, you can put this code include it anywhere in pages or in sidebar. (Warning: you may have to change the path on that as well, depending on what directory you put your file in)

<? include("/counter.php"); ?>

Here is some useful information about mode and these functions, which may be helpful for you:

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.

Functions

  1. Int fopen (string filename, string mode, int use_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.
  4. 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.
  5. Use the PHP fgets() function to find the number of hits there and save it in a variable.
  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. If the counter.dat file doesn’t exist, then use the fopen() function and declare it as a variable.
  9. Use the Fputs() function and put a “1″ in there.
  10. Print out that 1 person has come to the page and then close the file.

[download#3#image]

eel free to leave comments below so that i know how useful this post for you, thanks for visiting.

LEAVE A REPLY | VIẾT LỜI BÌNH