简体   繁体   English

如何使用 PHP 和 JavaScript 使图像可点击,并增加存储为平面文件的计数器?

[英]How can I use PHP and JavaScript to make an image clickable, and increment a counter stored as a flat file?

Im trying to find a php/js script that will let me take an image, and when clicked, increase the number in a flat file, and save that file.我试图找到一个可以让我拍摄图像的 php/js 脚本,当点击时,增加平面文件中的数字,然后保存该文件。

I know how to include the file to get the vote total.我知道如何包含文件以获得投票总数。

Im going insane trying to find this to plug and play into my website.我疯了,试图找到这个插件并在我的网站上播放。 Id love to have ip logging, and a cool fade in/out refresh update thing.我喜欢有 ip 日志记录,以及一个很酷的淡入/淡出刷新更新。 But at this point ill settle for basics.但在这一点上不能满足于基础。

Id like to avoid using MySQL, but if its necessary i can work with it.我想避免使用 MySQL,但如果有必要,我可以使用它。

Your best bet is to use the AJAX support in jQuery to access, but not load to the user, some kind of URL that writes the increment to the file.最好的办法是使用 jQuery 中的 AJAX 支持来访问,但不加载给用户,某种将增量写入文件的 URL。 If you're using any kind of a thorough platform, you should consider doing in the with your database.如果您正在使用任何类型的全面平台,您应该考虑使用您的数据库。 However, it'd be simple enough to use jQuery's $.get() function to access the URL /increment_number.php?image=whatever.jpg.但是,使用 jQuery 的 $.get() 函数访问 URL /increment_number.php?image=whatever.jpg 就足够简单了。 If you ever start using a database, you'd just have to change this script to perform a DB query.如果您开始使用数据库,则只需更改此脚本即可执行数据库查询。 For your case, you'd have a simple script like this (which has been in no way optimized or has any security considerations whatsoever):对于您的情况,您将有一个像这样的简单脚本(从未进行过优化或有任何安全考虑):

$image = $_GET['image'];
$number = file_get_contents('tracker_for_{$image}.txt');
if ($number != ''){
    $number = (int) $number + 1
}
$file = fopen('tracker_for_{$image}.txt', 'w');
fwrite($file, $number);
fclose($file);

And just remember to have the following bit of JS on the page with the image:只需记住在带有图像的页面上添加以下 JS 代码:

$(document).ready(function(){
     $('img.incrementme').click(function(){
         $.get('/increment.php?'+$(this).attr('src'));
     });
);

I haven't tested this code so it might not work, but it's in the spirit of what you'd have to do.我还没有测试过这段代码,所以它可能不起作用,但它本着你必须做的精神。

Something simple like this won't work?像这样简单的东西行不通?

<?php
// Link to this file: <a href='onclick.php'><img src='yourimg'></a>
$count = file_get_contents("count.file");
$count += 1;
file_put_contents("count.file", $count);

// Possibly log an IP too? open a file
$f = fopen("ipaddresses.file", "a");
fwrite($f, $_SERVER["REMOTE_ADDR"] . "\n");
fclose($f);
?>

If you are doing this for a voting system like Stack Overflow, creating lots of files to store this one bit of information is going to become unwieldy.如果您为 Stack Overflow 之类的投票系统执行此操作,则创建大量文件来存储这一位信息将变得笨拙。 This is perfect for a database.这对于数据库来说是完美的。

That way, you also wouldn't include the file, but perform a query to get the total score.这样,您也不会包含该文件,而是执行查询以获取总分。

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

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