我在这里有一个小脚本,可以在单击链接时计算点击次数并将其存储在 .txt 文件中,但是当我在 href 下只有“click=yes”时它可以正常工作。但是当我有指向外部网站的链接时,我无法跟踪点击次数。
这是我的代码:
<?php
if(!file_exists('counter.txt')){
file_put_contents('counter.txt', '0');
}
if($_GET['click'] == 'yes'){
file_put_contents('counter.txt', ((int) file_get_contents('counter.txt')) + 1);
header('Location: ' . $_SERVER['SCRIPT_NAME']);
die;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>counter example</title>
</head>
<body>
<h1><?php echo file_get_contents('counter.txt'); ?></h1>
<a href="http://www.google.com?click=yes" target="new">clickMe</a>
</body>
</html>
我的猜测是它必须与 header('Location: ' . $_SERVER['SCRIPT_NAME']); 但我无法弄清楚,所以我真的可以使用一些帮助。
是否有可能将多个链接保存到同一个文件中,当我在网站上显示它时,它是从最大到最小排序的?我有一个想法如何使用 MySQL 数据库,但我不能在将要实现它的地方使用它。
提前致谢!干杯!