dongxin9759 2014-12-19 05:10
浏览 35

会话变量未保留设置

Basically I am trying to set a session variable as a boolean to ensure a user cannot complete a function twice.

session_start()
if(isset($_GET['vote'], $_GET['title'], $_GET['user']))
{
    if(isset($_SESSION[$_GET['title']]))
    {
        $_SESSION[$_GET['title']] = true;
    }
    else if(!isset($_SESSION[$_GET['title']]))
    {
        $_SESSION[$_GET['title']] = false;
        add_title_vote($_GET['title'], $_GET['vote'], $_GET['user']);
    }
}

This code is supposed to take the information that was sent in a get method, then based on the one get variable, title, a new session variable is created. If the session variable already existed, it is supposed to set the variable to true. If the variable did not exist, the session variable is set to false and a function dependent on the value of the session variable is called. The function is only called if the session variable is false.

The function called if the session variable is false:

function add_title_vote($title, $vote, $user)
{
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "Assignments";
    $connection = new mysqli($servername, $username, $password, $dbname);
    if($_SESSION[$title] == false)
    {
    if($vote === 'up')
    {
    $sql = "UPDATE `mi4` SET `relevance` = `relevance` + 1 WHERE `title`='$title'";
    $servername1 = "localhost";
    $username1 = "root";
    $password1 = "";
    $dbname1 = "Users";
    $connection1 = new mysqli($servername1, $username1, $password1, $dbname1);
    mysqli_query($connection1, "UPDATE `login` SET `score` = `score` + 1 WHERE `user` = '$user'");
    }
    else if ($vote === 'down' && $votedown[$title] == false)
    {
    $sql = "UPDATE `mi4` SET `relevance` = `relevance` - 1 WHERE `title`='$title'";
    $servername1 = "localhost";
    $username1 = "root";
    $password1 = "";
    $dbname1 = "Users";
    $connection1 = new mysqli($servername1, $username1, $password1, $dbname1);
    mysqli_query($connection1, "UPDATE `login` SET `score` = `score` - 1 WHERE `user` = '$user'");
    }
    mysqli_query($connection, $sql);
    $_SESSION['voted'] = true;
    header("Location: /mi4.php");
    die();
    $_SESSION[$title] = true;
    }
}

For some reason it always says that the session variable has not been set, and executes the else if portion of the if statement in the first snippet of code.

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • douba5540 2014-12-19 06:11
    关注

    Just copy and past this. It works.

    session_start();
    if(isset($_GET['vote'], $_GET['title'], $_GET['user']))
    {
        if(isset($_SESSION[$_GET['title']]))
        {
            $_SESSION[$_GET['title']] = true;
        }
        else if(!isset($_SESSION[$_GET['title']]))
        {
            $_SESSION[$_GET['title']] = false;
            add_title_vote($_GET['title'], $_GET['vote'], $_GET['user']);
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用