简体   繁体   中英

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. 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.

You've written die() before you set the session:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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