i want to count the number of the clicks of a link and post the click count . i use the following code but it displays count as 0 . i can't figure the mistake where it is . however i can guess that the "Clicks" is not implemented properly in .. kindly help me please.
<?php
if( isset($_POST['clicks']) ) {
incrementClickCount();
}
function getClickCount()
{
return (int)file_get_contents("clickcount.txt");
}
function incrementClickCount()
{
$count = getClickCount() + 1;
file_put_contents("clickcount.txt", $count);
}
?>
<html>
<head>
<title>Click Count</title>
</head>
<body>
<a href="http://www.google.com" name="clicks">Google</a>
</ br>
</ br>
<div>Click Count: <?php echo getClickCount(); ?></div>
</body>
</html>
You are not posting with the click variable, so it will never run. Have you considered using Google analytics or some other analytics sollution?
What you are making here, is most valuable as a hit counter. Though it's very easy to manipulate it, as you cant filter out repeated hits by user, or search engines in a simple way.
If you want to make a script that logs hits to external links, you need some sort of "banner exchange" logging. I would make this with databases, so you can records hits per outgoing link. Such a sollution I would make with an "out"-script (as you see facebook and others have).
If you still wish to make this "log" function / hit counter, you will need to trigger it properly. Either change how it triggers, or post the variables upon the hyperlink (or change to form). You can trick the post via JQuery or Vanilla JS, yet a form would be more robust.