Jump to content

[SOLVED] hit counter problem


squiblo

Recommended Posts

with my hit counter, every time i refresh the page the hits go up by one everytime, but it should only go up by one the first time the page is viewed because the ip address is stored in a .txt file, and if the ip address is already stored then the hit should stay the same, also everytime i refresh the ip is stored again when it should only be stored once, i don't understand where i am going wrong  :confused:

 

<?php
	//connect to db
mysql_connect("localhost","","") or die ("Couldn't connect");
mysql_select_db("") or die ("Couldn't find db");

$query = mysql_query("SELECT * FROM members WHERE pageid='$pageid'");
$row = mysql_fetch_assoc($query);

//get current hits
$hits = $row['counter'];

//get ip file
$ipfile = file("ips.txt");
$linecount = count($ipfile);

//set defaul increment to true
$increment = true;

//this is the persons ip address
$ip = $_SERVER['REMOTE_ADDR']. $pageid;

//start loop
for($x=0;$x<=$linecount;$x++)
{
//does ip match an ip in ips.txt
if(intval($ipfile[$x]. $pageid) == $ip)
 $increment = false;



}
//if it matches
if ($increment == true)
{
$newhit = $hits + 1;
mysql_query("UPDATE members SET counter='$newhit' WHERE pageid='$pageid'");

$filenewip = fopen("ips.txt",'a');
fwrite($filenewip,$ip."\n");
}

echo $newhit;
?>

Link to comment
Share on other sites

what is $pageid? i never see it defined anywhere in that script. btw you want your for loop to have x < $linecount, because since arrays are zero based, once you hit the actual value of line count, (say its 5) you will get an array index that is out of bounds (or in PHP, it will be a null value, i believe)

 

for example, say you have an array with a length of 5, it will look like

0 : value

1 : value

2 : value

3 : value

4 : value

 

if you try to pass an index of 5 into that array, there will be no value for it

Link to comment
Share on other sites

"$ipfile[0]" is echoing the first line on the .txt file,

 

this is what the file looks like...

86.165.117.2321

86.165.117.2321

86.165.117.2321

 

this is what i echoed...

(in the loop)...

echo intval($ipfile[$x]. $pageid) . " == " . $ip . "<br />";

 

(out of the loop)

echo $ipfile[0];

 

and this is what returned...

86 == 86.165.117.2321

86 == 86.165.117.2321

1 == 86.165.117.2321

86.165.117.2321

Link to comment
Share on other sites

<?php

//connect to db

mysql_connect("","","") or die ("Couldn't connect");

mysql_select_db("squiblo_com") or die ("Couldn't find db");

 

$query = mysql_query("SELECT * FROM members WHERE pageid='$pageid'");

$row = mysql_fetch_assoc($query);

 

//get current hits

$hits = $row['counter'];

 

//get ip file

$ipfile = file("ips.txt");

$linecount = count($ipfile);

 

//set defaul increment to true

$increment = true;

 

//this is the persons ip address

$ip = $_SERVER['REMOTE_ADDR']. $pageid;

 

//start loop

for($x=0;$x<=$linecount;$x++)

{

//does ip match an ip in ips.txt

if(intval($ipfile[$x].$pageid) == $ip)

$increment = false;

 

echo ($ipfile[$x].$pageid) . " == " . $ip . "<br />";

 

}

//if it matches

if ($increment == true)

{

$newhit = $hits + 1;

mysql_query("UPDATE members SET counter='$newhit' WHERE pageid='$pageid'");

 

$filenewip = fopen("ips.txt",'a');

fwrite($filenewip,$ip."\n");

}

 

echo $ipfile[0];

?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.