Ready to get started?Download WordPress


How To Set and Use a Session Variable? (10 posts)

  1. zigx
    Posted 8 years ago #

    I am trying to use a session var in order to pass the value of the var around my blog but i cant figure out how to get my session to work basically.

    This is what i have so far:

    In wp-config.php:

    $_SESSION["ses_id"] = $_GET["the_id"];

    This is my plugin:

    function replace_with_the_id($content)
    //replaces in RSS feed
    if (is_feed())
    $content = str_replace("%my_id%", $_GET['the_id'], $content);
    //replaces in content of blog with value of ses_id
    if ($_SESSION["ses_id"])
    $content = str_replace("%my_id%", $_SESSION['ses_id'], $content);
    return $content;

    The RSS part works perfectly and does not need to use any kind of session stuff, but the post content need to use the session var and when i use this method the var is empty...

    any ideas? thoughts? insults :) ?

    I am not looking for someone to just code the whole thing out for me -- any input would be of great help, thank you.

  2. zigx
    Posted 8 years ago #

    Is it possible that this is a server setting i need to look into ?

    thank you.

  3. Pizdin Dim
    Posted 8 years ago #

    It should work as you have it. I have just tried your plugin code as well as the session stuff in "wp-config.php". I added the following line to a sample post:

    "BTW: the value you passed in the _SESSION[ses_id] is %my_id%."

    I then entered this URI, specifying the _GET paramater:


    What I got displayed in the post is this:

    "BTW: the value you passed in the _SESSION[ses_id] is 52."

    Which proves that it works.

    It may be the way your "php.ini" file is configured, although I don't know what specifically you should look for other than to see if your webserver allows session handling.

  4. zigx
    Posted 8 years ago #

    pizdin_dim, i really appreciate you taking the time to test this out.

    I see your result too... but this is the type of problem i see:

    - access site.com/wordpress/post-title/?the_id=blah, and this prints out "blah" fine
    - click "home" or something so im at site.com/wordpress/
    - access another post and "blah" will no longer print out

    Do you see this behavior too? or maybe know why i see it?

  5. zigx
    Posted 8 years ago #

    also, here is a screen shot from phpinfo() of my session config... anybody know what it should be set to, if that is in fact the problem?


  6. Pizdin Dim
    Posted 8 years ago #

    Ok, now I see what you're trying to do. Change the session assignment in "wp-config.php" to say this instead:

    if (isset($_GET['the_id'])) {
       $_SESSION['ses_id'] = $_GET['the_id'];

    That way, you're only overwriting the $_SESSION variable with the $_GET variable if the latter is specified.

  7. zigx
    Posted 8 years ago #

    ohhhhh mAN! that makes SO much more sense!! :X i will try it out and report back. :)

  8. zigx
    Posted 8 years ago #

    AH HAAA~!!! you are so the man!

    post your email and i will paypal you $5 for the help!!!

  9. Pizdin Dim
    Posted 8 years ago #

    Thanks. How about you give $10 to your favourite charity instead?

  10. zigx
    Posted 8 years ago #

    I actually have about $1k of stuff bagged up waiting for me to take it to a local charity.

    regardless, thank you very much.

Topic Closed

This topic has been closed to new replies.

About this Topic