duan1983 2014-02-22 09:09
浏览 29
已采纳

PHP显示链接的点击次数

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>
  • 写回答

1条回答 默认 最新

  • douzi1350 2014-02-22 11:25
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解