Custom Session Handler not working on PHP5.6 but d

2019-09-08 04:09发布

I am writing a Wordpress plugin that uses sessions and have written my own custom session handler to save session data in a custom table in the wordpress database.

I have the following defined in my main plugin file

add_action( 'init','start_session',1);    
function start_session() {
        if(!session_id())
        {
            session_set_save_handler('open_session', 'close_session', 'read_session', 'write_session', 'destroy_session', 'clean_session');
            session_start();
        }
    }

My session functions look like this

function open_session()
{
    return true;
}

function close_session()
{
    return true;
}

function read_session($sessionid)
{
    global $wpdb;

    $session_table_name = $wpdb -> prefix . "sessions";

    $query = $wpdb->prepare(
                        "SELECT data FROM $session_table_name
                        WHERE id = %s",
                        $sessionid);

    $result = $wpdb -> get_var($query);

    if ($result)
    {
        return $result;
    } else
    {
        return '';
    }
}

function write_session($sessionid,$data)
{
    global $wpdb;

    $session_table_name = $wpdb -> prefix . "sessions";

    $rowsaffected = $wpdb->replace(
                            $session_table_name,
                            array(
                                    'id' => $sessionid,
                                    'data' => $data
                            ),
                            array(
                                    '%s',
                                    '%s'
                            ));

    return true;
}

function destroy_session($sessionid)
{
    global $wpdb;

    $session_table_name = $wpdb -> prefix . "sessions";

    $rowsaffected = $wpdb->delete($session_table_name,array('id' => $sessionid),array('%s'));

    $_SESSION = array();

    return true;
}

function clean_session($expire)
{
    global $wpdb;

    $session_table_name = $wpdb -> prefix . "sessions";

    $wpdb->query(
        $wpdb->prepare(
                "DELETE FROM $session_table_name
                WHERE DATE_ADD(last_accessed, INTERVAL %d SECOND) < NOW()",
                $expire
        )
    );

    return true;
}

I have some pages created by the plugin, which I then replace the content for by adding a filter to the_content and checking the page ID.

        add_filter( 'the_content', 'content_filter' );

    function content_filter ($content)
    {
    ...
        if ($post->ID == $page_id)
        {
            $content = '';
            $content = basket_page($content);
        }
        return $content;
    }

   function basket_page($content)
   {
       $_SESSION['test'] = 'test';

       $content = $_SESSION['anexistingvariable'];

       return $content;
   }

Now this all works fine when running PHP7, however in PHP5.6 if I set a SESSION variable within the function basket_page() I'm getting a fatal PHP error that says 'Call to a member function replace() on null...' on line 39 of the session functions. This is the line that calls the replace method on $wpdb and is because the global $wpdb is empty.

What's weird is I am using Ajax in this plugin too, and writing session variables within my ajax callback functions is working fine even in PHP5.6. I can also read session variables from basket_page(), it just seems to be writes that are causing the problem.

My hosting provider only goes up to PHP5.6, so I need to get this working in that version. Any insight anyone can give would be greatly appreciated!

1条回答
Luminary・发光体
2楼-- · 2019-09-08 05:03

I have fixed this now myself, I needed to add a session_write_close(); to the wp_footer action hook. Everything now works in both PHP5.6 and PHP7. Hooray!

查看更多
登录 后发表回答