TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-17-2010, 03:01 PM   #1 (permalink)
The Wanderer
 
Join Date: Jan 2010
Posts: 10
Thanks: 4
Monferno is on a distinguished road
Default 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?
Monferno is offline  
Reply With Quote
Old 01-17-2010, 04:21 PM   #2 (permalink)
The Wanderer
Newcomer 
 
Parvus's Avatar
 
Join Date: Aug 2008
Posts: 21
Thanks: 1
Parvus is on a distinguished road
Default

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.
Parvus is offline  
Reply With Quote
Old 03-09-2010, 08:33 PM   #3 (permalink)
The Wanderer
 
Join Date: Feb 2010
Posts: 5
Thanks: 0
Aiahoos is on a distinguished road
Default

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");
?>
Aiahoos is offline  
Reply With Quote
The Following User Says Thank You to Aiahoos For This Useful Post:
Hashimi (03-11-2010)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using a simple counter harryhood General 6 04-13-2005 01:38 PM


All times are GMT. The time now is 02:49 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design