PHP SESSION variable troubles

I'm sorry to trouble you, I have tried my best to solve this but I am not sure where I am going wrong and I was wondering if someone out there would be willing to help!

Basically, I am having some issues with $_SESSION variables; I would like for each occasion that a visitor came to the page that they would be shown a different content message.. The below code, when first landing on a page will seem to skip the first "content1", and will display "content2" instead, then "content3" after another revisit. I've put in an unset call, which eventually sends it there, am I not using _SESSIONS correctly?

I'm not sure how the session variable was assigned to 1, for it to land correctly in the if===1 statement without it first returning the original "content1"

if (empty($_SESSION)) {
        session_start();
    }

    if (!isset($_SESSION['content'])) {
        $content = "content1";
        $_SESSION['content'] = 1;
        return $content;
    }
    elseif ($_SESSION['content'] === 1) {
        $content = "content2";
        $_SESSION['content'] = 2;
        return $content;
    }
    elseif($_SESSION['content'] === 2) {
        $content = "content3";
        unset($_SESSION['content']);
        return $content;
    }

Apologies for babbling or whether this was a simple fix / misunderstanding on my part. It's caused quite a headache!

Many thanks.

-edit-

This is a function that is called from within the same class, it has not gone through a loop anywhere either..

Answers


You should always use the session_start() function. If a session exists, it will continue it, otherwise it will create a new session.

Your code can then be simplified to the following:

// Start/Resume session
session_start();

// Get content
function display_message()
{
    if ( ! isset($_SESSION['content']))
    {
        $_SESSION['content'] = 1;
    }

    if ($_SESSION['content'] == 1)
    {
        ++$_SESSION['content']; // Increment session value
        return 'Content #1';
    }
    elseif ($_SESSION['content'] == 2)
    {
        ++$_SESSION['content']; // Increment session value
        return 'Content #2';
    }
    elseif ($_SESSION['content'] == 3)
    {
        unset($_SESSION['content']); // Remove session value
        return 'Content #3';
    }
}

// Display content
echo display_message();

However, if someone visits your page a fourth time, they will be shown the first message again (because the session value is no longer tracking what they've been shown).

Perhaps this sort of functionality might be handled better with by using a cookie to track this information?


You are only calling session_start(); if the session has not been created.

What about the other times, when it's 1, or 2?

Call session_start(); regardless of your if (empty($_SESSION)) { statement


Need Your Help

Opening a binary output file stream without truncation

c++ visual-c++

I am trying to open a binary output file to which I need to append some data. I cannot output the data sequentially, so I need to be able to seek within the file stream and cannot use the std::ios:...