简体   繁体   English

如何计算定位标记的点击次数?

[英]How to count clicks of an anchor tag?

I have a anchor tag: 我有一个锚标签:

<a href="abc.php?a=1">Count Click</a>

I want to count number of times this link has been clicked. 我想计算该链接被点击的次数。

In abc.php I use the following code but it is not working: 在abc.php中,我使用以下代码,但无法正常工作:

$b = $_GET['a'];
$b += 1;
echo "Number of Times Clicked=".$b;

EDIT Use a POST instead; 编辑使用POST代替;

$a = intval($_POST['a']);

session_start();
$b = intval($_SESSION['clickCount']);
$b += $a;
$_SESSION['clickCount'] = $b;
session_write_close();

You should store the value of this variable somewhere, because when you send GET request the page is refreshing and your value is missing. 您应该将此变量的值存储在某处,因为当您发送GET请求时,页面正在刷新并且您的值丢失了。

You can store this value in session. 您可以将该值存储在会话中。 See http://php.net/manual/features.sessions.php or cookies see http://php.net/manual/features.cookies.php or in some database on server. 请参阅http://php.net/manual/features.sessions.php或cookie,请参阅http://php.net/manual/features.cookies.php或服务器上的某些数据库。

And then get it from there. 然后从那里获取。

First of all recommend you to store values in $_SESSION and also use $_POST 首先,建议您将值存储在$_SESSION ,并使用$_POST

Why use $_SESSION & $_POST ? 为什么要使用$_SESSION$_POST because no one can mess up with your application. 因为没有人可以弄乱您的应用程序。

Basic Example (PHP): 基本示例(PHP):

<?php
session_start();
$varCount = 0;
if(isset($_POST['submit'])){
    $var = intval($_POST['var']);
    $addVar = $var+1;
    $_SESSION['newVar'] = $addVar;
}
else{
    unset($_SESSION['newVar']);
}
if(isset($_SESSION['newVar'])){
    $varCount = intval($_SESSION['newVar']);
    echo $varCount;
}
?>

HTML: HTML:

<form method="post" action="">
    <input type="hidden" name="var" value="<?=$varCount?>">
    <input type="submit" name="submit" value="Add">
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM