自定义会话处理程序不适用于 PHP5.6 但适用于 PHP7 [英] Custom Session Handler not working on PHP5.6 but does on PHP7

查看:41
本文介绍了自定义会话处理程序不适用于 PHP5.6 但适用于 PHP7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用会话的 Wordpress 插件,并编写了我自己的自定义会话处理程序来将会话数据保存在 wordpress 数据库的自定义表中.

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();
        }
    }

我的会话函数是这样的

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;
}

我有一些由插件创建的页面,然后我通过向 the_content 添加过滤器并检查页面 ID 来替换其内容.

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;
   }

现在这在运行 PHP7 时一切正常,但是在 PHP5.6 中,如果我在函数 bag_page() 中设置了一个 SESSION 变量,我会收到一个致命的 PHP 错误,显示调用成员函数 replace() on null"...' 在会话函数的第 39 行.这是在 $wpdb 上调用 replace 方法的行,因为全局 $wpdb 是空的.

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.

奇怪的是,我也在这个插件中使用了 Ajax,即使在 PHP5.6 中,在我的 ajax 回调函数中编写会话变量也能正常工作.我还可以从 bag_page() 读取会话变量,它似乎只是导致问题的写入.

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.

我的托管服务提供商只升级到 PHP5.6,所以我需要让它在那个版本中工作.任何人都可以提供任何见解,我们将不胜感激!

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!

推荐答案

我现在自己解决了这个问题,我需要添加一个 session_write_close(); 到 wp_footer 动作挂钩.现在一切都适用于 PHP5.6 和 PHP7.万岁!

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!

这篇关于自定义会话处理程序不适用于 PHP5.6 但适用于 PHP7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆