Manage sessions in wordpress plugin
Posted by shadow_of__soul | Posted in Uncategorized | Posted on 01-02-2009
0
Hi,
i’m doing a lot of things in wordpress the last time
my last project was the integration of a script what i made before in a wordpress blog. the script are a custom shopping cart, and it use $_SESSION vars to manage products, and a unique ID for the cart.
so i thinked it going to be easy, a simple wrap plugin what call’s to the respective php. my surprise has been when i tested it and all the sessions are empty WTF?
so searching over the net for a solution, i get what it’s the problem. the thing it’s wordpress erase all the $_SESSION vars calling to a function named wp_unregister_GLOBALS and it’s defined in wp-settings.php
if you have the register_globals off, you dont need to worry, because then the function dont erase the vars but if not, you need to hack the function.
so, change this:
$noUnset = array(‘GLOBALS’, ‘_GET’, ‘_POST’, ‘_COOKIE’, ‘_REQUEST’, ‘_SERVER’, ‘_ENV’, ‘_FILES’, ‘table_prefix’);
into this:
$noUnset = array(‘_SESSION’,'GLOBALS’, ‘_GET’, ‘_POST’, ‘_COOKIE’, ‘_REQUEST’, ‘_SERVER’, ‘_ENV’, ‘_FILES’, ‘table_prefix’);
after this, in your plugin, add a hook to fire the session_start() function:
add_action(‘init’,’sessionset’);
function sessionset(){
if(!session_id()){
session_start();
}
}
with that you have the all your $_SESSION vars available in your plugin
i hope you find this useful
Regards,
Shadow.




