Hit counter
Moderator: General Moderators
Hit counter
Hello everybody,
Anyone can help me in hit counter? I need get the hit of each page for my side...
Thanks a lot!
Anyone can help me in hit counter? I need get the hit of each page for my side...
Thanks a lot!
Re: Hit counter
What have you tried so far?
Re: Hit counter
<?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
$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
Can you be more specific?rei27 wrote:This is what i have tried so far, but cant work
Re: Hit counter
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?
<? include("conter.php");?>
But the problem is it doesn't work, so any other code to do this?
Re: Hit counter
Can you be more specific?rei27 wrote:But the problem is it doesn't work
Re: Hit counter
You means my question or what?
Re: Hit counter
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
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.
I could keep on going but it's your job, I can only give you ideas.
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
}
}
?>Re: Hit counter
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
You have commented the echo function so ofcourse the script will not give any output sent :p
Re: Hit counter
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.
I could keep on going but it's your job, I can only give you ideas.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 } } ?>
Thanks, i really need some idea about it!