Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Counter, grab ip address

Status
Not open for further replies.

shawkz

Programmer
Oct 25, 2005
84
GB
Hi, i want to be able to add a hidden hit counter to my web page in php. Also to grab the ip address of the user accessing the web page. I want it to only increment the counter once per visit (not for each page they access). Not sure of the best way to do this. Please can anyone help!

Kindest regards,

SH
 
something like this?

Code:
session_start();
//grab the IP address
$ip = $_SERVER['REMOTE_ADDR'];

//check whether there is already a session element for IP
if (isset($_SESSION['IP'])){
  if ($_SESSION['IP'] == $ip) {
    //do nothing as the ip is already stored
    $hits = getHits();
  } else {
    //store the IP in the session data
    $_SESSION['IP'] = $ip;
    $hits = addHit();
  }
}

//display  $hits if you want

function addhit(){
  $hc = 'hitcounter.txt'; //filename to store the counter number
  $lf = 'hc.lock'; //a variable for the lock file
  //get the current hit counter value
  $cnt = getHits();
  $newcnt = $cnt + 1;
  $sleepcounter = 0;// protection from stuck lock files

  //add a write lock
   while (is_file($lf)) {
     usleep (10); 
     if ($sleepcounter >= 100) {
       unlink($lf);
     }
     $sleepcounter++;
   }
   $fl = fopen($lf, "wb");
   fwrite($fl, '££');
   fclose ($fl);
   //open the file for WRITING
   $fh = fopen($hc, "wb");
   // write the new figure
   fwrite ($fh, $newcnt);
   //close the file
   fclose($fh);
   unlink ($lf); //delete the write lock file
   //return the new hitcount
   return $newcnt;
 }
}
function getHits(){
 
  $hc = 'hitcounter.txt'; //filename to store the counter number
  //grab the contents of the file and cast to an integer
  $cnt = (int) file_get_contents($hc);
  return $cnt;
}

include this at the top of every page. or put it in a separate file and add the file to the auto_prepend directive in php.ini.

note: have not checked the above and typed it straight into the tt box so it may have errors/bugs
 
Grief, that looks good. Will have a play with it and let you know how i get on! Does this increment the counter for each page thats loaded?

many thanks,

SH
 
Hmmm, i found an extra } on line 49. Removed it. It runs with no errors. But the text file doesn't get any bigger than 2k. I have chmod 777 the file.
 
nope. it increments only for a new IP address in that session. so a person could visit 100 pages within a session and that would be one hit.

a session can technically be carried to a different IP address, but the code picks this up and logs it as a fresh hit. you can prevent session portability by locking down the php.ini file a bit, but this in turn prevents session persistency for users that have disabled cookies.

as written the session lasts until the browser is closed.

the code has some write locking built in, which bulks it out. the write locking is probably only relevant if your site is really busy and so likely to get multiple hits per second. the danger is to make sure that the file does not get overwritten with zero data if a file read does not work. there are ways to protect against it, i chose a file locking mechanism with a 1 second time out - which is not perfect but there you go!

make sure that your webserver (or the php process) has write and read access to the directory in which you will be storing the write lock and hitcounter files.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top