Hit counter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rei27
Forum Commoner
Posts: 27
Joined: Thu Apr 30, 2009 4:17 am

Hit counter

Post by rei27 »

Hello everybody,
Anyone can help me in hit counter? I need get the hit of each page for my side...

Thanks a lot!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Hit counter

Post by requinix »

What have you tried so far?
rei27
Forum Commoner
Posts: 27
Joined: Thu Apr 30, 2009 4:17 am

Re: Hit counter

Post by rei27 »

<?php

$filename = 'hitcount.txt';
$handle = fopen($filename, 'r');
$outbuffer = '';
$found = false;
$hits = 1;

// reading data from file
while ( !feof( $handle ) )
{
$buffer = trim( fgets( $handle ) );
if ( strlen( $buffer ) > 0 )
{
list( $page, $count ) = explode( '|', $buffer );
if ( !$found && $page == $_SERVER[PHP_SELF] )
{
$hits = $count + 1;
$outbuffer .= $_SERVER[PHP_SELF] . '|' . $hits . "\n";
$found = true;
}
else
{
$outbuffer .= $buffer . "\n";
}
}
}
fclose($handle);

if (!$found)
{
$outbuffer .= $_SERVER[PHP_SELF] . "|1\n";
}

// writing updated data to file
$handle = fopen($filename, 'w');
fwrite($handle, $outbuffer);
fclose($handle);

// Uncomment the next line (remove //) to display the number of hits on your page.
//echo $hits;

?>


This is what i have tried so far, but cant work
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Hit counter

Post by requinix »

rei27 wrote:This is what i have tried so far, but cant work
Can you be more specific?
rei27
Forum Commoner
Posts: 27
Joined: Thu Apr 30, 2009 4:17 am

Re: Hit counter

Post by rei27 »

the code i posted just now located in counter.php, after that i include the php file into the page i wish to code like the following:

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

But the problem is it doesn't work, so any other code to do this?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Hit counter

Post by requinix »

rei27 wrote:But the problem is it doesn't work
Can you be more specific?
rei27
Forum Commoner
Posts: 27
Joined: Thu Apr 30, 2009 4:17 am

Re: Hit counter

Post by rei27 »

You means my question or what?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Hit counter

Post by requinix »

I mean you say it "can't work" and "won't work" and you don't explain what that means. What doesn't work? What is it supposed to do? What does it actually do? Why is it wrong?
SvanteH
Forum Commoner
Posts: 50
Joined: Wed Jul 08, 2009 12:25 am

Re: Hit counter

Post by SvanteH »

Does it generate any errors? Do you know if you get the input of the file for starters? Why not e.g load in the whole textfile then exploding it into an array then looping through that array and altering it.

Code: Select all

<?php
  $hitCounter = file_get_contents("hitcounter.txt");
  $arrHitCounter = explode("\n\r", $hitCounter);
  
  foreach($arrHitCounter as $rowHitCounter)
  {
    //$i as indentifier to see which array item row we are in
    $i++;
    $arrRowData = explode("|", $rowHitCounter);
    if (($arrRowData[0]) == ($_SERVER["PHP_SELF"]))
    {
      //found line
    }
  }
?>
I could keep on going but it's your job, I can only give you ideas.
rei27
Forum Commoner
Posts: 27
Joined: Thu Apr 30, 2009 4:17 am

Re: Hit counter

Post by rei27 »

tasairis wrote:I mean you say it "can't work" and "won't work" and you don't explain what that means. What doesn't work? What is it supposed to do? What does it actually do? Why is it wrong?

I mean it cant count the user hit. By right it should show me the number of hits, but it doens't.
SvanteH
Forum Commoner
Posts: 50
Joined: Wed Jul 08, 2009 12:25 am

Re: Hit counter

Post by SvanteH »

You have commented the echo function so ofcourse the script will not give any output sent :p
rei27
Forum Commoner
Posts: 27
Joined: Thu Apr 30, 2009 4:17 am

Re: Hit counter

Post by rei27 »

SvanteH wrote:Does it generate any errors? Do you know if you get the input of the file for starters? Why not e.g load in the whole textfile then exploding it into an array then looping through that array and altering it.

Code: Select all

<?php
  $hitCounter = file_get_contents("hitcounter.txt");
  $arrHitCounter = explode("\n\r", $hitCounter);
  
  foreach($arrHitCounter as $rowHitCounter)
  {
    //$i as indentifier to see which array item row we are in
    $i++;
    $arrRowData = explode("|", $rowHitCounter);
    if (($arrRowData[0]) == ($_SERVER["PHP_SELF"]))
    {
      //found line
    }
  }
?>
I could keep on going but it's your job, I can only give you ideas.

Thanks, i really need some idea about it!
Post Reply