0

我正在尝试为数据库创建投票系统,当前记录在屏幕上以 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);
}
4

1 回答 1

-1

用于会话的默认序列化程序无法处理纯数字键。它还会在关机期间发出警告消息,您应该在日志中看到它(假设您启用了日志记录,请查找未知)。

旧的序列化处理程序不能存储数字索引,字符串索引也不能包含特殊字符

(引用以下链接)

您可以使用以下脚本对其进行测试,将其保存在某处并刷新几次:

<?php
session_start();
$_SESSION[time()] = true;
var_dump($_SESSION);

最便携的解决方案是使用静态字符串为键添加前缀。或者,您可以更改用于会话的序列化处理程序。

于 2013-08-24T16:11:37.720 回答