简体   繁体   English

会话变量未保留

[英]Session variable is not remaining set

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. 该代码应采用在get方法中发送的信息,然后基于一个get变量title来创建一个新的会话变量。 If the session variable already existed, it is supposed to set the variable to true . 如果会话变量已经存在,则应将其设置为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. 如果该变量不存在,则将会话变量设置为false并调用依赖于该会话变量的值的函数。 The function is only called if the session variable is false . 仅当会话变量为false时才调用该函数。

The function called if the session variable is false : 如果会话变量为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. 由于某种原因,它总是说尚未设置会话变量,并在第一段代码中执行if语句的else if部分。

Thanks in advance. 提前致谢。

You've written die() before you set the session: 在设置会话之前,您已经编写了die()

    mysqli_query($connection, $sql);
    $_SESSION['voted'] = true;
    header("Location: /mi4.php");
    die(); <--
    $_SESSION[$title] = true; 
    }
}

You can either remove it or put it after session. 您可以删除它,也可以在会话后放置它。 Both ways you don't need it because you're not doing anything with it. 两种方式都不需要它,因为您没有对其进行任何操作。

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']);
    }
}

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

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