WordPress.org

Ready to get started?Download WordPress

Forums

session variables + function ouput = PROBLEM (4 posts)

  1. volleyballmaniac
    Member
    Posted 6 years ago #

    I enabled sessions by adding a session start function to my wp-config file.

    Since then, I have happily been able to pass session variables between pages.

    Then came the day that I decided that I needed to store the output of functions into session variables. And that's where I hit a problem.

    -------------------------

    I CAN assign session variables (like 1 or 'Hello World')using the following code:

    <?php
    $_SESSION['JoeCatID'] = 1;
    ?>

    I can then retrieve it on another page using this code:

    <?php
    echo $_SESSION['JoeCatID'];
    ?>

    -------------------------------
    PROBLEM

    but when I attempt to call a function and assign the function output into the session variable, like so:

    <?php
    $_SESSION['JoeCatID'] = get_query_var('cat');
    ?>

    I am no longer able to retrieve the session variable on another page (using the same retrieve code above).

    --------------------------------------

    WEIRD SYMPTOMS WHICH MIGHT HELP
    - I am able to assign and retrieve the function assigned session variable, if I assign & retrieve at the same time on one page load (place both functions on page).

    - but if I try to assign on one page, and then retrieve on another (with the session variable retrieve), the variable does not return properly.

    - If the function returns an INT (like get_query_var('cat')), on next page, it returns a "0".

    - If the function returns a string (like category_description()), on next page, it returns nothing (blank).

    - I even tested by assigning the function assigned session variable on Page 1, reloading Page 1 (to assign it); I then removed the assignment call, and replaced it with the retrieve call, and it fails.

    - so regardless of whether I call it on the Same Page, or a new page, the session is failing when I try to retrieve a function assigned session variable AFTER the Page Load.

    - however (as I said above) it works perfectly when I assign, and then try to retrieve a non-function variable (like 1 or 'Hello World').

    --------------------------

    Those are all the details about the issue. I've been working for 2 days straight, but I can't seem to resolve it.

  2. volleyballmaniac
    Member
    Posted 6 years ago #

    I also tried calling the function first, and then assigning it to $_SESSION after, like so:

    <?php
    $foo = get_query_var('cat');
    $_SESSION['JoeCatID'] = $foo;
    ?>

    but that doesn't work either (same symptoms as noted in first posting).

  3. volleyballmaniac
    Member
    Posted 6 years ago #

    SOLVED!

    this is quite possibly the strangest error/bug I have ever dealt with, with WordPress (or perhaps its PHP).

    I had an animated Favicon Gif file in my header.php.
    <link rel="icon" href="animated_favicon1.gif" type="image/gif" >

    Once I removed that, my session problem was solved.

    I don't know how the heck that changed anything; quite honestly, I was just desperate so I just started removing things one by one to see if it fixed the problem.

  4. satranc
    Member
    Posted 5 years ago #

    Hi,

    You cant use Session variables at WordPress.
    http://blog.ginchen.de/en/2008/08/15/session-variablen-in-wordpress/

Topic Closed

This topic has been closed to new replies.

About this Topic