繁体   English   中英

单击链接时如何运行PHP计数器脚本?

[英]How can I run a PHP counter script when clicking a link?

我有一个脚本,当我在网站上访问页面时会运行该脚本,它会更新值并将其存储在文本文件中,但是当我在网站上单击链接时我想运行相同的脚本,此链接只是打开一个文件在我的Web目录中,而不是实际页面中。 这是脚本:

<?php
$count_my_page = ("hitcounter.txt");
$hits = file($count_my_page);
$hits[0] ++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");
fclose($fp);

?>

因此,如果我想在单击链接时运行此程序,是否有人知道我将在哪里执行此操作? 这是我的链接的示例:

<li><a href="files/file.pdf" target="blank" title="File">File</a></li>

因此,此链接会在新标签页中打开该文件,我正竭尽全力试图从链接单击中找出如何运行此文件,我们将不胜感激。

我当时正在考虑调用一个PHP函数,该函数在单击链接时运行代码,但我做不到。

非常感谢!

将计数脚本更新为:

<?php
$count_my_page = "hitcounter.txt";
$hits = file($count_my_page);
$hits[0] ++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");
fclose($fp);

header("Location: ".$_GET['file']");
?>

并使用链接:

<a href="count.php?file=files/file.pdf" target="blank" title="File">File</a>

更新您的代码

另存为名称count.php

<?php
file_put_contents('hitcounter.txt', ((int) file_get_contents('hitcounter.txt')) + 1);
header('Location: ' . $_GET['url']);
?>

连结至网路

<a href="count.php?url=http://askedboss.com" target="blank">CLICK HERE</a>

暂无
暂无

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

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