我有一个网站http://www.gfcf14greendream.com/我正在发布我的程序和游戏。我编写代码来下载我制作的程序没有问题(单击“程序”按钮然后发送SMS Sender),但我对如何生成计数器感到困惑。在此之前,我不想在网站上使用它,因为我真的想自己格式化。我想这样做的方法是使用一个txt文件,smssender.txt只有:
0
然后处理覆盖txt文件的网站的javascript是:
$("#downbutton").click(function () {
downcounter++;
if (downcounter == 1) $("#counter").text("SMS Sender has been downloaded " + downcounter + " time...");
else $("#counter").text("SMS Sender has been downloaded " + downcounter + " times...");
$.post("http://www.gfcf14greendream.com/PHP/smssender.php", {
counter: downcounter
}, function (data) {});
});
应该调用一个PHP文件smssender.php,它只有5行:
<?php
$counter = $_POST['counter'];
file_put_contents("/counters/smssender.txt", $counter);
?>
我希望我知道是否正在调用php页面,因为更改指示页面中下载时间的文本的代码运行良好,但是一旦页面刷新,下载次数就会恢复为0,因为0是从这段代码中获得的:
var downcounter = 0;
$.get("http://www.gfcf14greendream.com/counters/smssender.txt", function (data) {
downcounter = data;
if (downcounter == 1) $("#counter").text("SMS Sender has been downloaded " + downcounter + " time...");
else $("#counter").text("SMS Sender has been downloaded " + downcounter + " times...");
});
这清楚地表明没有覆盖发生,因为它成功地从smssender.txt中检索0(我之前尝试过1和2并且它有效)。那为什么代码错了呢?真的很感激任何形式的帮助。感谢您的时间,祝大家新年快乐!
更新
我尝试将javascript函数的代码更改为:
var txtfile = "http://www.gfcf14greendream.com/counters/smssender.txt";
$.ajax({
type:'POST',
url: 'PHP/increment.php',
data: txtfile,
success: function() {
alert("Download incremented");
$.get("http://www.gfcf14greendream.com/counters/smssender.txt", function(data){
downcounter = data;
if (downcounter == 1) $("#counter").text("SMS Sender has been downloaded " + downcounter + " time...");
else $("#counter").text("SMS Sender has been downloaded " + downcounter + " times...");
});
}
});
并添加了一个PHP文件increment.php,其中包含您在此处提供的一些代码:
<? php
$file = $_POST['txtfile'];
$counter = intval(file_get_contents($file));
$counter++;
file_put_contents($file, $counter);
?>
但是,仍然没有运气。这段代码会起作用还是我误用了引用?谢谢
答案 0 :(得分:5)
这是一种构建计数器的简单方法,但它有一些问题。 1
不应依赖客户端的JavaScript为counter
提供值,而应完全在服务器端处理。例如,如果两个用户同时打开页面,则两者都以相同的值开头,但只增加一个而不是正确递增一个每个。
此方法还避免了清理客户端发送并保存在文件中的计数器值的需要,并且无需根据需要处理任何帖子。相反,只需调用脚本就可以增加计数器。
// Does nothing but read and increment the file:
// Read the value
$current_counter = intval(file_get_contents('/path/outside/web/root/counters/smssender.txt'));
// Increment it
$current_counter++;
file_put_contents('/path/outside/web/root/counters/smssender.txt', $current_counter);
// Output it to the client
echo $current_counter;
JavaScript的责任只是调用 PHP脚本,没有参数。
$.get("http://www.gfcf14greendream.com/PHP/smssender.php", function(data) {
// Do something on success
console.log(data);
});
为了防止smssender.txt
文件的直接请求,建议将该文件存储在Web服务器文档根目录之外。在file_get_contents()/file_put_contents()
中提供文件的正确路径。
要检索当前计数器值并将其发送到客户端,您只需要读取并回显该值。
echo intval(file_get_contents('/path/outside/web/root/counters/smssender.txt'));
最后,请注意,此处没有任何内容可以阻止任何人通过一遍又一遍地调用smssender.php
脚本来运行计数器。如果您有登录信息,则应确保用户在访问计数器时已登录。如果没有登录,您应该考虑在下载实际发生时设置cookie值,这样您就可以断言用户在计数器递增时实际下载了当前会话中的内容。
if (isset($_COOKIE['some-download-value'])) {
// Update the counter.
}
如果您的Web主机不允许您将文件放在Web服务器文档根目录之外,则可以使用.htaccess中的以下规则阻止直接HTTP请求到.txt文件:
# Block direct access to any .txt file
<Files ~ "\.txt$">
order allow,deny
deny from all
</Files>
或者您可以将以下内容放在counters/
目录中的.htaccess文件中:
# counters/.htaccess
# block all HTTP requests to counter files
Order deny,allow
Deny from all
1 一个更简单的方法是避免所有文件存储,读取,写入和安全问题,它将计数器值存储在数据库表中,并在每次访问时UPDATE
存储。