5

I have followed this guide: http://philpalmieri.com/2009/06/codeigniter-and-wordpress-play-well-together/

Which basically says, to install wordpress, get it working, then replace the index.php file with Code Igniters, and then at the bottom of the file right before we initiate CodeIgniter, require the wp-load file of word press.

Works fine.

However now, my $_SESSION doesn't work. I have set code igniter to use Database sessions, and its logging the session values, but It still doesn't work. I can't log into my CodeIgniter systems admin panel, I can't do much of anything that requires sessions because Sessions dont work. LOL.

How to fix this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
David Lawrence
  • 659
  • 1
  • 8
  • 15
  • I should note. If I require the Wordpress file within the controller function, it works, minus the fact that site_url is defined twice and PHP doesnt like that. Any ideas maybe on how to change site_url? – David Lawrence Apr 14 '12 at 16:10
  • Going to try: http://wordpress.org/extend/plugins/wp-code-igniter/installation/ – David Lawrence Apr 14 '12 at 16:18

2 Answers2

4

I did the following to get this to work (I have a Code Igniter application in a separate directory within a Wordpress directory) -- what I obviously don't like about it is that I had to modify a core file within Wordpress.

First, I added my Code Igniter cookie name to the $no_unset array in wp-includes/load.php In my case it was ci_session:

    $no_unset = array( 'GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES', 'table_prefix','ci_session' );

Second, I figured out that Wordpress's add_magic_quotes function was mangling the $_COOKIE global. This was causing CodeIgniter to re-create the cookie on each page load or redirect and thereby breaking any continuity. So, I commented this line out in wp-includes.load.php (around line 545)

//$_COOKIE = add_magic_quotes( $_COOKIE );

Next, to keep this function in tact for all other Wordpress related cookies, I created an array_walk function to loop over the $_COOKIE global and apply add_magic_quotes to all cookies except for mine within the wp-includes/load.php function

/**
* Applies Magic Quotes to the $_COOKIE global but ignores Codeigniter's Cookie
* @param  string $value Value passed by array_walk function
* @param  string $key   Key passed by array_walk function
*/
function ci_ignore_magic_quotes($value,$key)
{
    if($key != "ci_session")
    {
        stripslashes_deep($value);
    }
}

//Put this line in place of the commented out line above...
array_walk($_COOKIE, 'ci_ignore_magic_quotes');

After I did this, I was no longer having multiple cookies stored in my ci_sessions table and sessions were successfully retained.

I hope this helps!

Phil Birnie
  • 1,104
  • 2
  • 12
  • 29
1

There's an explanation here of how WordPress unsets session variables, and a possible solution. Unfortunately it seems to require modifying core files - there don't seem to be any hooks in wp_unregister_GLOBALS that would help.

Hobo
  • 7,536
  • 5
  • 40
  • 50
  • I should have mentioned that I tried that. I tried adding "_SESSION" to the $notunset variable, and I tried commenting out wp_unregister_GLOBALS() all together, still no luck. I tried turning off register_globals through PHP.ini. I tried uncommenting wp_unregister_GLOBALS, etc. I tried the opposite. I tried pretty much everything I could. I even tried a few WP Plugins, as well as adding session_start() to wp-config (the top of the file) and adding a function that had session_start() to the active themes function.php file. – David Lawrence Apr 15 '12 at 00:48
  • All you need to have is `register_globals` disabled, than `wp_unregister_GLOBALS` won't unset any session variable. That's the recommended setting anyway. Disable it. – hakre Apr 15 '12 at 16:14
  • Yea it didnt help anything. Still cant use cookies or sessions. – David Lawrence Apr 16 '12 at 18:03