Click counter

Hello, how to make click counter with IP checking ?

Do you want to make one or are you looking for a ready-made one? If it is the latter check out Hot Scripts.

Thanks for answer,

I am looking ready made, i need to make 2 buttons for example A and B
when i press A button it counts the A button click and you cant click A or B anymore by the same IP. So i am looking for a script with something like click counter. but with ip cheking.

I think this is not much work to do. You need to have a database table to store the A/B and clicked IP address. Then when you show the links for click (A/B), just check whether the IP address and the clicked value already exist or not. If exist then don’t show the texts A/B to click otherwise show. If you don’t know how to track the user’s IP address see $_SERVER super global array.

What about those with the same IP or an ever changing IP address?

What if your script that made the page the button resides on created a random check-code, which is passed back to the click counter, which in turn checks that check-code, along with the IP address, to reduce the possibility of denying multiple users with the same IP address? Heck, with just a unique check-code, you don’t really need the IP addy.

Or… you can just use a session var. :smiley:

Kind of what I was getting at, though I was leaning more toward using a database. Of course, bypassing a session could be as easy as clearing a cookie, but… :smiley:

i dont know how to do it… because i am new in programing. i know a little bit php. maybe there is some scripts which are similar ?

Here’s a working example for you to study. You can see it in action at

http://www.geekcavecreations.com/clickCount.php


<?php
session_name("clickCount");
session_start();
$warn1 = "Click the button to count your click<br />\
";
$warn2 = $warn3 = $warn4 = $warn5 = "";
$checkCode = mt_rand();
$warn2 =  "Stored check code    = $checkCode<br />\
";
$buttonDisabled = (isset($_SESSION['disabled']) and $_SESSION['disabled'] === "true") ? 'disabled="disabled"' : '';
$count = (file_exists("clickCount.dat")) ? file_get_contents("clickCount.dat") : 0;
$_SESSION['checkCode'] = (isset($_SESSION['checkCode'])) ? $_SESSION['checkCode'] : $checkCode;
if (isset($_POST['submit'])) {
  $postedCheckCode = $_POST['checkCode'];
  $cc = $_SESSION['checkCode'];
  $warn2 =  "Stored check code    = $cc<br />\
";
  $warn3 =  "Submitted check code = $postedCheckCode<br />\
";
  if (!empty($buttonDisabled)) $warn1 = "Button is disabled!<br />\
";
  if (empty($buttonDisabled)) {
    // perform your click count tasks here. Example code follows:
    $warn4 = "Processing Click Count...<br />\
";
    $count++;
    $dummyVar = file_put_contents("clickCount.dat",$count);
  }
  if ($cc !== $postedCheckCode) {
    $_SESSION['disabled'] = "true";
    $buttonDisabled = 'disabled="disabled"';
  }
}
if (!empty($buttonDisabled)) $warn1 = "Button is disabled!<br />\
";
$warn5 = "Count = $count<br />\
";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Click Count Example</title>
  </head>
  <body>
    <h3>&nbsp; Click Count Test</h3>
    <pre>
<?php echo $warn1 ?>
<?php echo $warn2 ?>
<?php echo $warn3 ?>
<?php echo $warn4 ?>
<?php echo $warn5 ?>
    </pre>
    <form name="clickCount" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
      <input type="hidden" name="checkCode" value="<?php echo $checkCode ?>" />
      <input type="submit" name="submit" <?php echo $buttonDisabled ?> value="clickCount" />
    </form>
  </body>
</html>

Note: Yes, I know that presentation and logic are intertwined. While this is generally frowned upon, it’s only an example to study, and can be coded properly once the concepts shown have become understood. :smiley:

I forgot to mention that you can “get around” this method by simply deleting the cookie for this page. Other than that, however, it’s pretty workable, since re-submitting the data will not increment the count, unless cookies are disabled. But even then, the button is disabled. I’d put in a header re-direct, to prevent form re-submission, but that would just re-enable the button, so the script isn’t perfect, but it gets the point across.

Thank you a lot :wink: il try to study and if will be questions i`ll write :wink:

I have one question :slight_smile:

I want to make these buttons in every article so for this i need a lot of files to count… maybe with database it will be easier?

No.