0

I'm trying to set a session variable to record when people have voted but PHP is flat out refusing to set it. The code is as follows:

elseif (isset($_GET['group']) && isset($_GET['vote']))
    {
        include_once(_INC.'otherheader.php');
        groupVotePage($group, $vote);
        $_SESSION[$group] = '1';
        echo $_SESSION[$group];
    }

Nothing. The function groupVotePage adds the vote to the database and echoes a thanks message. $group is the name of the group being voted for. I have session_start(); at the top of the page and have tried to declare the variable inside the function called as well, putting session_start(); everywhere. Session variables are used elsewhere on the site so I know it's not a server issue, and it's the same on all browsers I tried.

Declaring the session var inside the function works but only within the function - it doesn't go global.

 if(!isset($_SESSION[$group])) {
        $totalVote=$totalVote+$vote;
        $totalNumVotes=$totalNumVotes+1;
        $totalRating=round($totalVote/$totalNumVotes);
        $totalScore=$totalVote*$totalNumVotes;
        ...db stuff...

        $mysqli->query($query);

        echo'Thanks for voting!';
    }

    else {
        echo'You have already voted for this group!';
    }
5
  • Are you sure it's ever reaching that condition? Commented Feb 11, 2016 at 1:22
  • You should show where $group is getting its value and consider doing a var_dump on $_SESSION. Commented Feb 11, 2016 at 1:24
  • Both $group and $vote are user selected and properly escaped, the whole function runs like clockwork so I know variables are being passed. Declaring the session var inside the function works but only within the function - it doesn't go global. Commented Feb 11, 2016 at 1:28
  • Your comment about putting session start everywhere is interesting. I would expect to see error messages due to trying to start the session more than once. Would you interested in ensuring that the session is always started once only and that you will see all errors? if so follow the instructions: Create a simple application bootstrap file. Remove all session_start(); instructions from your current script. Now, 1) session is always started 2) you will see errors. Treat notices as errors and fix them.Then we can fix what is wrong with your current script. Commented Feb 11, 2016 at 2:21
  • maybe interesting? In another question with session issues similar to yours, I put a website together, with simple code, that looks after sessions: Website: rfv123.eu.pn/tok. Clear current tok value. Destroy Session and Session Cookie. Source code: index.php and token.php. Original Question: session php rand other values. Commented Feb 11, 2016 at 12:06

2 Answers 2

0

Maybe you can use PHP error reporting code to figure out where you going wrong

// Report all errors
error_reporting(E_ALL);
Sign up to request clarification or add additional context in comments.

Comments

0

Okay fixed it, the < !DOCTYPE HTML> section was before the session_start();, when I put it after the code worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.