Wordpress in CodeIgniter

2019-02-17 17:44发布

问题:

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?

回答1:

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!



回答2:

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.