Announcement

Collapse
No announcement yet.

PHP Counter Problem Needed Help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP Counter Problem Needed Help

    Hi, programmer
    How to count the number of the time and the page has been acessed?
    Free and Open Source PHP counter scripts I recommended can only count the number of times the page has been accessed. It cannot count the number of unique visitors. The PHP hit counter simply updates a text file, which keeps track of the number of times the page is accessed. The PHP Script read the current the file, then adds one to the number, writes the number to the file and close the file. The script is then embedded into the html on the web page so that it is executed each time the page is accessed.
    The hit counter script looks like this:
    PHP Code:
    <?php
    $filename 
    "hits.txt";
    $countfile($filename);
    $count[0]++;
    $file fopen ($filename"w") or die ("Cannot find $filename");
    fputs($file"$count[0]");
    fclose($file);
    echo 
    $count[0];
    ?>
    The filename variable holds the name of the text file with the hit count in it. When you add the script to your web page, you should also add a text file called "hits.txt" with only the number "0" in it to the directory where your page is held.

    The $count variable accesses the value with the "file" function. This function reads the entire file into an array. In this case the array will only have one value. The line "$count[0]++;" increases the value by one.

    The file is then opened with the "fopen" function. The "w" in that function allows the file to have write access. If the file is not there, it will return the text "Cannot find hits.txt" and the script will end. If the file is opened successfully, the "fputs" function will write the new $count value to the file. The file is then closed with the "fclose" function.

    The last line of the script, "echo $count[0];" writes the hit count to the screen. If you don't want the hit count to be visible, you can either leave that line out, or comment it out by placing a # symbol at the beginning of the line.
    Embedding the Code
    # Save the code as "hits.php" and place it in the same directory as your web page. If your web server is a Linux server, you should make sure both the "hits.php" is world executable and the "hits.txt" file is world writable by typing the commands "chmod 755 hits.php" and "chmod 777 hits.txt" at a command prompt.

    Call the "hits.php" script from within your web page by placing the following line into the html code:

    <?php include("hits.php"); ?>

    You can test your code by reloading your web page. If the counter increases, you know that you have set everything up correctly. If it does not, check your code for typos and make sure there is a semicolon at the end of each line.
    PHP Groupware | PHP template

  • #2
    As you want to count the number of unique visitors you just need to create 5 files and here are the steps to count the number of unique visitors:

    1) empty index file.
    Just make an empty file and name it as index.html. After creating the file just Save it in a folder called counter. If you don't have the folder counter then just create it.

    2) ip.db
    Now we need to create an empty file called ip.db. Actually this is a file where visitors' IPs
    will be stored. Save this file also in the folder called.

    3) count database
    The count database file should contain set of zeros which should be as follows:
    0%0%0%0000 00 00%0
    Where the first zero will represents today's count. The second zero will be yesterday's count and the third zero will be the total count. The next set of 8 zeroes will be today's date, and whereas the last
    zero will indicate for how many days has the script been working. Please note that these values will
    change on script execution, so you need to leave them as they are and then name this file as
    count.db and save it also in the counter folder.
    Please make sure that if your website has been online for some time and you already been using some free counter and if you'd like to keep that number then replace the third zero with the total visitor count and the last number with the number of days you used the script for, else your average statistics will be incorrect.


    4) counter display
    The next step is to make a new file and save it as showcounter.php. Because this file will open your count database and will display the statistics. For this you need to copy the following
    code to your file:

    <table>
    <tr>
    <th colspan="2">Unique visitors</th>
    </tr>
    <tr>
    <td><b>Today</b></td>
    <td>
    <?php
    $file_count = fopen('counter/count.db', 'rb');
    $data = '';
    while (!feof($file_count)) $data .= fread($file_count, 4096);
    fclose($file_count);
    list($today, $yesterday, $total, $date, $days) = split("%", $data);
    echo $today;
    ?>
    </td>
    </tr>
    <tr>
    <td><b>Yesterday</b></td>
    <td>
    <?php
    echo $yesterday;
    ?>
    </td>
    </tr>
    <tr>
    <td><b>Total</b></td>
    <td>
    <?php
    echo $total;
    ?>
    </td>
    </tr>
    <tr>
    <td><b>Daily average</b></td>
    <td>
    <?php
    echo ceil($total/$days);
    ?>
    </td>
    </tr>
    </table>

    This code will create a table with header Unique visitors Then, it will display the words Today,
    Yesterday, Total, Daily average in the left column, and the numeric values will be displayed in the right column.
    The number 4096 is max number of bytes used to read from the file, but you must make sure that you leave it as it is.
    Save this file as-well in the folder counter.


    5) counter script
    The next part that comes is to make a new file and name it counter.php. Save this also in the counter folder along with all the other files that you have made so far. And copy the following code:

    <?php
    $ip = $_SERVER['REMOTE_ADDR'];

    $file_ip = fopen('counter/ip.db', 'rb');
    while (!feof($file_ip)) $line[]=fgets($file_ip,1024);
    for ($i=0; $i<(count($line)); $i++) {
    list($ip_x) = split("\n",$line[$i]);
    if ($ip == $ip_x) {$found = 1;}
    }
    fclose($file_ip);

    if (!($found==1)) {
    $file_ip2 = fopen('counter/ip.db', 'ab');
    $line = "$ip\n";
    fwrite($file_ip2, $line, strlen($line));
    $file_count = fopen('counter/count.db', 'rb');
    $data = '';
    while (!feof($file_count)) $data .= fread($file_count, 4096);
    fclose($file_count);
    list($today, $yesterday, $total, $date, $days) = split("%", $data);
    if ($date == date("Y m d")) $today++;
    else {
    $yesterday = $today;
    $today = 1;
    $days++;
    $date = date("Y m d");
    }
    $total++;
    $line = "$today%$yesterday%$total%$date%$days";

    $file_count2 = fopen('counter/count.db', 'wb');
    fwrite($file_count2, $line, strlen($line));
    fclose($file_count2);
    fclose($file_ip2);
    }
    ?>


    Now the final step is Making it work


    If you have followed all the above given steps then you should be having 5 files under your folder called counter and make sure that this folder should be placed inside your sites
    root directory.

    Then now to make the counter work, you have to include the scripts in your site pages.
    Please be aware that if you are using dynamic inclusion, then you will have to include the counter only in your main file, called index.php. And if you don't use dynamic inclusion, then you will have to include the script in each of your file. You can include the counter by adding following command:

    <?php include 'counter/counter.php'; ?>

    The Counter will be invisible, but it will be working. If you want to make your statistics
    visible, all you need to do is just paste the below given code whereever you want the counter to appear:

    <?php include 'counter/showcounter.php'; ?>

    Now as you have linked everything, you can test it on your server.

    Just upload the entire folder with counter scripts and data along with the modified files
    where you included the script to your root folder. Then, just go to the folder counter and change the permission of the files files count.db and ip.db to 666.

    Now go to your page where statistics are shown it is the the page where you have included
    showcounter.php and there you should see a number 1 or higher for today,
    and zero for yesterday. And if you don't see the counter exactly where you expected, then
    it means that you did something wrong. Then you need to follow the complete steps all over again.

    Hope this information helps you out in solving your problem.....

    Comment


    • #3
      Thanks!
      It is great helpful for me!
      PHP Groupware | PHP template

      Comment


      • #4
        Thank you for updating. It really sounds good that this information seems to be helpful for you and you are satisfied with it. We do our best to help you out with any queries and get your issue resolved If you need any assistance or if you have any questions, please do not hesitate to get back to us by email or live chat or even you can PM me. We are always available round the clock to assist you technically regarding the issue you may experience with...

        Comment


        • #5
          If you are using cPanel, you can simply set the counters for the domain from your cPanel. For this you can go to cPanel >> Software/Services >> CGI Center >> Counter . Also you can customize the colors, styles, max number of digits etc from here.

          Helpful for the people who are not much aware of programming.
          Sarina,
          Linux Support Department.

          Comment


          • #6
            Originally posted by Sarina View Post
            If you are using cPanel, you can simply set the counters for the domain from your cPanel. For this you can go to cPanel >> Software/Services >> CGI Center >> Counter . Also you can customize the colors, styles, max number of digits etc from here.

            Helpful for the people who are not much aware of programming.
            That suits me fine as I am a bit of a programming novice

            Many thanks

            Comment

            Working...
            X