DokuWiki

It's better when it's simple

User Tools

Site Tools


Sidebar

Translations of this page?:

Learn about DokuWiki

Advanced Use

Corporate Use

Our Community


Follow us on Facebook, Twitter and other social networks.

Our Privacy Policy

devel:session_handling

Session handling

DokuWiki opens the session in inc/init.php. After some things are initialized it is closed again. This is because PHP locks the session and this session lock may halt background requests like images, ajax, etc.

Depending on when your plugin runs, the session might already have been closed. You can still read a closed session's variables but writing will have no effect. If you need to write to it, you need to call session_start() again, use the session as usual and close the session with session_write_close() again. To re-open a session, no chars must be sent to the clients browser.

You can also act before the session is closed, for example when catching the DOKUWIKI_STARTED event in an Action plugin:

class action_plugin_demo extends DokuWiki_Action_Plugin {
    function register(Doku_Event_Handler $contr) {
        $contr->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'session_update');
    }
    public function session_update(Doku_Event $event, $param) {
        if (isset($_POST['demo_persistent_data']) && checkSecurityToken()) {
            if (!empty($_POST['demo_persistent_data'])) {
                $_SESSION["my_persistent_data"] = $_POST['demo_persistent_data'];
                // optionally saves the persistent data in a cookie
                set_doku_pref("my_persistent_data",$_POST['demo_persistent_data']);
            } else {
                unset($_SESSION["my_persistent_data"]);
                // optionally removes an associated cookie
                set_doku_pref("my_persistent_data",false);
            }
            return;
        }
        // This is for all the pages of the site, other than handling the above update
        // Optionally loads the cookie
        if (!isset($_SESSION["my_persistent_data"])) {
            $_SESSION["my_persistent_data"] = get_doku_pref("my_persistent_data","");
        }
    }
}

This can be used for example in the syntax plugin:

class syntax_plugin_demo extends DokuWiki_Syntax_Plugin {
 
    // Connect and handle the keywords/tokens you recognize as usual
 
    // ...
 
    // main rendering code
    function render($mode, Doku_Renderer $renderer, $data) {
 
        // ... 
 
        // At some point, you can create a form to upload persistent data
        $renderer->doc .= '<p><form id="demopersist" enctype="multipart/form-data" method="post" action="">';
        $renderer->doc .= formSecurityToken();
        $renderer->doc .= 'Data: <input name="demo_persistent_data" />';
        $renderer->doc .= '<button type="submit">Make Persistent</button></form></p>';
 
        // ... 
 
        // In every page, you can read the persistent data simply by accessing the $_SESSION variable
        msg("TEST session ".$_SESSION["my_persistent_data"], 0);
    }
 
}

Obviously you should tighten the security more than in the above example, but it gives you a start.

devel/session_handling.txt · Last modified: 2020-11-08 11:25 by dokuwiki@nicolas.brodu.net