1

I have two pages. On one page I am adding an array to $_SESSION. On a second page, I am reading the array (successfully), and then unset that array from $_SESSION.

What happens however is that the second page shows different values than what the first page stored in the session - and that leads me to think that the first page is called again and again, but how can that be if I am on a session and not reloading the page again myself?

What am I doing wrong? Thanks

EDIT: Forgot to mention that the second script is requested by a flash file on the website. The flash file has the session ID as one of its parameters and then sends it to the second script to load the right session (the session_id($_REQUEST['token']); part).

Page 1:

$_SESSION['HEADER_MODEL'] = getModelForHeaderVideo();
/*
    [HEADER_MODEL] => Array
        (
            Yes these are just random numbers (for testing)
            [modID] => 303768959
            [modUsername] => 347073152
        )
*/

Page 2:

<?php
        session_id($_REQUEST['token']);
        session_start();

        header('Content-Type: text/html; charset=utf-8');

        if (!isset($_SESSION['HEADER_MODEL']['modID']) || (trim($_SESSION['HEADER_MODEL']['modID']) == '')) {
                echo "modID=0";
        } else {
                echo "&modID={$_SESSION['HEADER_MODEL']['modID']}&modUsername={$_SESSION['HEADER_MODEL']['modUsername']}";
                unset($_SESSION['HEADER_MODEL']);
        }
?>
4
  • 2
    i think the problem is with session_start()..Why are you starting a new session on page 2? Commented Sep 25, 2012 at 10:43
  • 1
    Do you know what session_id does? Commented Sep 25, 2012 at 10:44
  • I apologize. I forgot some important information. The second file is requested by a flash movie to get the values from. I am posting the session id to the second file to load the same session. I saw this solution somewhere yesterday. I added it to the original question. Commented Sep 25, 2012 at 10:47
  • @Jon Oh, I just read that: "When using session cookies, specifying an id for session_id() will always send a new cookie when session_start() is called, regardless if the current session id is identical to the one being set.". Is that what you were talking about? But then the array should be empty if it's a new session, and like I said, it's not. It keeps changing every time I load the second page. Commented Sep 25, 2012 at 10:53

1 Answer 1

1

Please Refer to the PHP documentation

Note: When using session cookies, specifying an id for session_id() will always send a new cookie when session_start() is called, regardless if the current session id is identical to the one being set.

PHP session array keeps changing because that is what your code instruct it to do

Sign up to request clarification or add additional context in comments.

3 Comments

Yes I just read about it - posted a comment about it in the above answer. However, upon page referesh, I am getting NEW values in the HEADER_MODEL array, that means is getting inserted into the session again. But how can that be? I am sorry but I don't understand.. Thanks.
Because the second page is called from a flash file. Is it not the correct way to do it?
Well, a more constructive comment would be an explanation about why it isn't the correct way.

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.