gpt4 book ai didi

php - 实现facebook风格的 "unlike"函数

转载 作者:行者123 更新时间:2023-11-29 14:01:14 25 4
gpt4 key购买 nike

我最近为我的漫画网站实现了自定义喜欢和不喜欢功能。我想让用户能够通过“取消单击”喜欢或不喜欢按钮来“收回”他们的选择。

我的函数的工作原理是:

1) Passing button value (id = 'like' or id = 'dislike') via Jquery to php script

2) script will first check if an ip exists in the database against that given comic id... if not it will insert user's IP and current comic ID... if it does, it originally said "you've already voted"... but now to implement "unliking", I will just have it run a delete query

3) then it will get total current likes for that comic id and increment.

我认为可以做到的方式是,如果用户再次按下按钮,我基本上运行相反的查询...从给定漫画 ID 的表中删除该用户的投票...然后减少该图像的总喜欢在漫画表中。

所以我的问题是

1) Is doing an insert query if they press a button once, then a delete query if they "deselect" that same choice the best way to implement this? Couldn't a user spam and overload the database by continuously pressing the like button, thereby constantly liking and unliking? Should I just implement some sort of $_SESSION['count'] for that ID?

2) If I'm storing a certain IP... what happens if several uniques users happen to use the same computer at... let's say a netcafe... it will always store that user's IP. Is storing against the IP the best way to go?

如果您需要引用,请输入代码:

<?php 
include 'dbconnect.php';
$site = $_GET['_site'];
$imgid = intval($_GET['_id']);
$input = $_GET['_choice'];

if ($site == "artwork") {
$table = "artwork";
}
else {
$table = "comics";
}

$check = "SELECT ip, tablename, imgid FROM votes WHERE ip = '".$_SERVER['REMOTE_ADDR']."' AND tablename = '$table' AND imgid = $imgid";
$result = $mysqli->query($check);

if ($result->num_rows == 0) {
//Insert voter's information into votes table
$sql = "INSERT INTO
votes (ip, tablename, imgid)
VALUES
(\"".$_SERVER['REMOTE_ADDR']."\", \"$table\", $imgid)
ON DUPLICATE KEY UPDATE
imgid = VALUES(imgid)";

if (!$mysqli->query($sql)) printf("Error: %s\n", $mysqli->error);

/*while ($row = $result->fetch_assoc()) {
echo "you've inserted: " . $row['ip'] . ", " . $row['tablename'] . ", " . $row['imgid'] . ".";
}*/

$result = $mysqli->query("SELECT like_count, dislike_count FROM $table WHERE id = $imgid");

//put the counts into a list
list($likes, $dislikes) = $result->fetch_array(MYSQLI_NUM);

if ($input == "like") {
$sql = "UPDATE $table SET like_count = like_count + 1 WHERE id = $imgid";
$mysqli->query($sql);
$likes++;
}
else if ($input == "dislike") {
$sql = "UPDATE $table SET dislike_count = dislike_count + 1 WHERE id = $imgid";
$mysqli->query($sql);
$dislikes++;
}
}
else { //"unlike" their previous like for that given image id
$sql = "DELETE FROM
votes
WHERE (ip, tablename, imgid) =
(\"".$_SERVER['REMOTE_ADDR']."\", \"$table\", $imgid)";

if (!$mysqli->query($sql)) printf("Error: %s\n", $mysqli->error);

$result = $mysqli->query("SELECT like_count, dislike_count FROM $table WHERE id = $imgid");

//put the counts into a list
list($likes, $dislikes) = $result->fetch_array(MYSQLI_NUM);

if ($input == "like") { //remove like
$sql = "UPDATE $table SET like_count = like_count - 1 WHERE id = $imgid";
$mysqli->query($sql);
$likes--;
}
else if ($input == "dislike") {
$sql = "UPDATE $table SET dislike_count = dislike_count - 1 WHERE id = $imgid";
$mysqli->query($sql);
$dislikes--;
}
}

echo "Likes: " . $likes . ", Dislikes: " . $dislikes;

mysqli_close($mysqli);

?>

最佳答案

1)我会说是的,使用计数功能来限制他们可以连续点击按钮的尝试次数。除非它们达到非常高的数字,否则可能不会有太多麻烦,我相信一个简单的循环就可以了。

2) 我不会只存储 IP。我会尝试使用 IP 以外的其他内容作为标识符,例如 IP 和 session cookie - 这样它是唯一的。然而,在回顾服务器时,您必须解析数据库中的条目。或者也许是 MAC 地址。我不确定您是否有权访问该内容。 How can I get the MAC and the IP address of a connected client in PHP?

我确信还有另一种方法,但从概念上讲,我认为这就是它的工作原理。

关于php - 实现facebook风格的 "unlike"函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15027420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com