Codeigniter session in wordpress

I have developed a website in codeigniter and integrated wordpress for blog. My problem is if i logged in the site, i am not able to use the codeigniter session value in the wordpress blog.

In Codeigniter once i logged in i am setting the following session using codeigniter session.

Read More
$this->session->set_userdata('get_user_session', $auD);

Now i need to use the session in wordpress header file or index file. I tried the below code in wordpress.

session_start();
print_r($_SESSION);

I am not getting the above ci session values.

Related posts

Leave a Reply

3 comments

  1. CodeIgniter’s Session class:

    >> stores session information for each user as serialized (and optionally encrypted) data in a cookie.

    The values you set using $this->session->set_userdata('key','value') are not going to be present in the $_SESSION variable.

    CodeIgniter also supports storing session content in a database.

    You can use that configuration to retrieve the session data in your WordPress blog.

  2. Use COOKIES to get the session data.
    Refer this link: https://thomashunter.name/blog/accessing-codeigniter-session-data-using-external-scripts/

    require_once("config.php");
    $cisess_cookie = $_COOKIE['ci_session'];
    $cisess_cookie = stripslashes($cisess_cookie);
    $cisess_cookie = unserialize($cisess_cookie);
    $cisess_session_id = $cisess_cookie['session_id'];
    
    $cisess_connect = mysql_connect(MYSQL_HOSTNAME, MYSQL_USERNAME, MYSQL_PASSWORD);
    if (!$cisess_connect) {
     die("<div class="error">" . mysql_error() . "</div>");
    }
    $cisess_query = "SELECT user_data FROM ci_sessions WHERE session_id = '$cisess_session_id' LIMIT 1";
    
    mysql_select_db(MYSQL_DATABASE, $cisess_connect);
    $cisess_result = mysql_query($cisess_query, $cisess_connect);
    if (!$cisess_result) {
     die("Invalid Query");
    }
    $cisess_row = mysql_fetch_assoc($cisess_result);
    $cisess_data = unserialize($cisess_row['user_data']);
    print_r($cisess_data);
    
  3. I use the following code in wp-config.php and works for me.

    Store session in Codeigniter in DB.

    Codeigniter version 3.x.x

    define('BASEPATH', 2021);
    require_once(__DIR__.DIRECTORY_SEPARATOR.'panel'.DIRECTORY_SEPARATOR.'application'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR."database.php");
    $cisess_cookie = $_COOKIE['ci_session'];
    
    $cisess_connect = mysqli_connect($db[$active_group]['hostname'], $db[$active_group]['username'], $db[$active_group]['password']);
    if (!$cisess_connect) {
        die("<div class='error'>" . mysqli_error($cisess_connect) . "</div>");
    }
    $cisess_query = "SELECT data FROM sessions WHERE id = '$cisess_cookie' LIMIT 1";
    
    mysqli_select_db($cisess_connect,$db[$active_group]['database']);
    $cisess_result = mysqli_query($cisess_connect,$cisess_query);
    if (!$cisess_result) {
        die("<div class='error'>" . mysqli_error($cisess_connect) . "</div>");
    }
    $cisess_row = mysqli_fetch_assoc($cisess_result);
    $session_data = $cisess_row['data'];
    
    $return_data = array();
    $offset = 0;
    while ($offset < strlen($session_data)) {
        if (!strstr(substr($session_data, $offset), "|")) {
            throw new Exception("invalid data, remaining: " . substr($session_data, $offset));
        }
        $pos = strpos($session_data, "|", $offset);
        $num = $pos - $offset;
        $varname = substr($session_data, $offset, $num);
        $offset += $num + 1;
        $data = unserialize(substr($session_data, $offset));
        $return_data[$varname] = $data;
        $offset += strlen(serialize($data));
    }
    print_r($return_data);
    exit;