WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Using PHP Sessions in WordPress (7 posts)

  1. Peter Wooster
    Member
    Posted 2 years ago #

    I've spent a bit of time sorting out the use of PHP sessions in WordPress. I've posted a discussion of the issues on my blog at http://devondev.com/2012/02/03/using-the-php-session-in-wordpress/

    For those who just want the bottom line here it is. Put the following code in your plugin or theme code:

    add_action('init', 'myStartSession', 1);
    add_action('wp_logout', 'myEndSession');
    add_action('wp_login', 'myEndSession');
    
    function myStartSession() {
        if(!session_id()) {
            session_start();
        }
    }
    
    function myEndSession() {
        session_destroy ();
    }

    Do not put the session_start in wp-config.php if there is any chance that register_globals is set in your PHP system.

    /peter

  2. Peter Wooster
    Member
    Posted 2 years ago #

    I have published this code as plugin Simple Session Support.

    There are other session plugins out there, but they do not clean up at logout.
    /peter

  3. kishores
    Member
    Posted 2 years ago #

    My php session unset due to wp_head()in header.php.My session works fine without wp_head(). Is there any conflict between session and wp_head?

  4. dino golman
    Member
    Posted 1 year ago #

    There is a powerful plugin for sessions here: WP Sessions Plugin

  5. Peter Wooster
    Member
    Posted 1 year ago #

    That looks like a useful plugin for those who need all that functionality. However it is a CodeCanyon premium plugin and as such doesn't belong here.

  6. lubchik
    Member
    Posted 1 year ago #

    Thank you!!! I was really helpful!

  7. tcreary
    Member
    Posted 1 year ago #

    I installed and activated the plugin but I cannot pass $_SESSION variables yet. Is there anything else required to initiate the add_action function?

Topic Closed

This topic has been closed to new replies.

About this Topic