Page 1 of 1

Hit counter

Posted: Tue Jul 07, 2009 10:41 pm
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!

Re: Hit counter

Posted: Tue Jul 07, 2009 10:44 pm
by requinix
What have you tried so far?

Re: Hit counter

Posted: Tue Jul 07, 2009 10:56 pm
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

Re: Hit counter

Posted: Tue Jul 07, 2009 11:11 pm
by requinix
rei27 wrote:This is what i have tried so far, but cant work
Can you be more specific?

Re: Hit counter

Posted: Tue Jul 07, 2009 11:37 pm
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?

Re: Hit counter

Posted: Tue Jul 07, 2009 11:49 pm
by requinix
rei27 wrote:But the problem is it doesn't work
Can you be more specific?

Re: Hit counter

Posted: Tue Jul 07, 2009 11:52 pm
by rei27
You means my question or what?

Re: Hit counter

Posted: Wed Jul 08, 2009 12:41 am
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?

Re: Hit counter

Posted: Wed Jul 08, 2009 1:01 am
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.

Re: Hit counter

Posted: Wed Jul 08, 2009 1:12 am
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.

Re: Hit counter

Posted: Wed Jul 08, 2009 1:13 am
by SvanteH
You have commented the echo function so ofcourse the script will not give any output sent :p

Re: Hit counter

Posted: Wed Jul 08, 2009 1:13 am
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!