我正在尝试为数据库创建投票系统,当前记录在屏幕上以 php 呈现,并带有图像以进行向上或向下投票。单击时,它们分别运行 php 脚本 upvote.php 或 downvote.php,它们传递 id 值(被操作记录的整数。
目前它有效,脚本按预期递增和递减记录投票值。但是,我试图阻止用户为一条记录多次执行此操作。我试图通过使用会话并将其命名为 id 的值并在更改投票值之前检查是否已设置该 id 的会话来实现这一点。
我将使用我的“upvote.php”作为示例:
//Upvote Script
//begin session
session_start();
//database connection credentials import
include("../scripts/connection_variables.php");
//connect to mysql or display error
@mysql_connect("$db_host","$db_username","$db_pass") or die ("Could not connect to the database, please try again shortly. If problem persists please refer to help then contact support.");
//select database or or display error
@mysql_select_db("$db_name") or die ("Could not connect to the database, please try again shortly. If problem persists please refer to help then contact support.");
//collect id
$id = $_GET['id'];
//check if user has already voted for this
if(isset($_SESSION[$id])) {
//session has been set for this id, so don't execute the script
exit();
}else{
//set the session
$_SESSION[$id] = "The punniest thing about puns is that they are really punny.";
//increment the votes value
$query = "UPDATE punniest_database SET votes= 1 + votes WHERE id='$id'";
mysql_query($query);
}