How to use session in wordpress in plugin development

I am new to write a plugin ..I am having a testplugin.php file and a ajax.php file ..

My code in testplugin.php is

global $session;

print_r($abc); //$abc is array of my data ..

$session['arrayImg']=$abc; //saving data in session 

echo  $session['arrayImg']; //displayin "Array"

And my ajax.php consists of following code

global $session;

$abc = $session['arrayImg'];

print_r ("abs== ".$abc); //displayin "abs== Array"

And if use session_start();

I get following error

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent 

I just want to send array of data from one file of my plugin to another file ...

Answers


// On your plugin or themes functions.php

function register_session(){
    if( !session_id() )
        session_start();
}
add_action('init','register_session');

// To set a SESSION data -

$_SESSION['arrayImg'] = $abc;

// To get the data on ajax hooked function -

function resolve_the_ajax_request(){
    if( !session_id())
        session_start();

    $abc = $_SESSION['arrayImg'];
}

In my case I was using that session variable in plugin activation as well. So did something unorthodox. Instead of defining my session_start in a hook I made it as the first line in my plugin :).

To heck with plugins, as soon as wordpress scans through my file it initiates the session.

At the end I do not destroy the session on user logout. I simply unset my variable. This is to just in case if some other plugin is also using session. If I destroy session it may affect other plugins.

Cheers.


Need Your Help