TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Hit Counter Help (http://www.talkphp.com/absolute-beginners/5228-hit-counter-help.html)

Monferno 01-17-2010 03:01 PM

Hit Counter Help
 
Hello

I have recently decided I was going to create a Hit Counter in PHP for my site when it goes up. Sadly when I run it it decided that it will not work for me.

The main file with all of the PHP coe in is called class.php. The contents of the file are:
PHP Code:

    function showHitCounter($file) {
    
        
$counterFile $file;
        
        print 
'<div id="hit-counter">';
            include(
$counterFile);
        print 
'</div>';
    
    }
    
    function 
hit($file) {
    
        
$T 'T';
        
        if (isset(
$_COOKIE['hit-today']) {
        
            print 
'<!-- This user has already had their hit counted today -->';
        
        }
        
        else {
        
            
$fo fopen($file'r+');
            
$contents fread($fo10)+1;
            
            
fwrite($fo$contents);
            
            
fclose($fo);
            
            
setcookie('hit-today''T'time()+86400);
        
        }
    
    } 

Basicly, in the index file, I call the file class.php and then run the hit function.

I have a live example http://seaglass-jewellery.com/example/HitCounter/ (here)

Can anyone help me?

Parvus 01-17-2010 04:21 PM

Well, your code isn't really great.

Your showHitCounter function prints a div but nothing in the div.
The function never counts the amount of lines in the file and never prints this, so that you see nothing is normal.

Your hit function has the variable $T which is never used and you check a hit with a cookie, which will never be the nice way for a hitcounter.
Why is a cookie bad ? Because a user can simply unset the cookie and visit again.


There are loads of hitcounters on the web that you can use, if your really want to code one yourself, then I suggest you first choose what you want to count.
Every page view ?
Every unique page view ?
A user can view a page once per day ?

If you simply want every page view, then you can simply save the number of hits and add 1 to it every view.
For every unique page view, you will have to save the IP off a viewer and you can select all lines from a file to count the amount of unique views.
And if you want a user to view a page once every x hours or days, you will have to save the IP with the Date of the view and check if the date is before the x hours or days.

Aiahoos 03-09-2010 08:33 PM

according to php tutorials you can create a counter text file of the name hitcounter.txt and save it to the same directory you will put your hit counter in.
Open your php editor. Save the document as counter.php. Set your opening statement for PHP script.
PHP Code:

<?php
?>

Set the variable for the file named hitcounter.txt and place this between your PHP tags. remember to place the filename in quotes. place the semicolon on the end of the line to end the command.
PHP Code:

<?php
$count_my_page 
= ("hitcounter.txt");
?>

Set the number of visits to the current value of the contents of the hitcounter.txt file:
PHP Code:

<?php
$count_my_page 
= ("hitcounter.txt");
$hits file($count_my_page);
?>

When the script is accessed as the page loads, it not only reads the current number of hits on the page in the hitcounter.txt, it must increase the value by 1 for the current hit to be recorded. Add 1 to your value of $hits and call it $hits.
PHP Code:

<?php
$count_my_page 
= ("hitcounter.txt");
$hits file($count_my_page);
$hits[0] ++;
?>

Open the file that is keeping count so we can write to it
PHP Code:

<?php
$count_my_page 
= ("hitcounter.txt");
$hits file($count_my_page);
$hits[0] ++;
$fp fopen($count_my_page "w");
?>

Replace the value in the file with the new $hits number after it was incremented.
PHP Code:

<?php
$count_my_page 
= ("hitcounter.txt");
$hits file($count_my_page);
$hits[0] ++;
$fp fopen($count_my_page "w");
fputs($fp "$hits[0]");
?>

You must close the file next.
PHP Code:

<?php
$count_my_page 
= ("hitcounter.txt");
$hits file($count_my_page);
$hits[0] ++;
$fp fopen($count_my_page "w");
fputs($fp "$hits[0]");
fclose($fp);
?>

Set the code to display your hit number.
PHP Code:

<?php
$count_my_page 
= ("hitcounter.txt");
$hits file($count_my_page);
$hits[0] ++;
$fp fopen($count_my_page "w");
fputs($fp "$hits[0]");
fclose($fp);
echo 
$hits[0];
?>

Save your php file in same directory as the text file.
To use this script you just have to add a php statement that calls that php file. Use the following include:
PHP Code:

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



All times are GMT. The time now is 09:28 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0