How to access Magento from CodeIgniter?

How to access Magento from CodeIgniter?

September 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to access Magento from CodeIgniter?

Integrating Magento into CodeIgniter: A Comprehensive Guide

Are you looking to access your Magento store from within a CodeIgniter application? This can be achieved by loading the Magento core files directly in your CodeIgniter controllers or models. Here, we’ll walk through a common scenario where a user attempts to retrieve customer data using both direct PHP and via an embedded CodeIgniter controller.

Directly Accessing Magento

When you run your Magento access script from the root directory (with .htaccess disabled), it can successfully retrieve information about the logged-in customer:

<?php
$site_root = '/var/www/mysite/www/httpdocs';
require_once ($site_root . '/store/app/Mage.php');
umask(0);
// ... // The rest of your initialization and data retrieval code here

echo $magento_message;
?>

The script initializes the Magento application, retrieves the customer session, and prints out a personalized greeting if a user is logged in. This works perfectly when run from the root directory.

Embedding Magento in CodeIgniter

However, things become more complex when you try to integrate this code within an embedded CodeIgniter controller or model:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Test_mage extends CI_Controller {

    function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        // ... // The same Magento initialization and data retrieval code as above

        echo $magento_message;
    }
}

The key issue here is that when CodeIgniter loads the script, it alters the execution environment in ways that Magento doesn’t expect. Specifically, it changes the session handling mechanism.

Diagnosing the Issue: CodeIgniter’s Session Hijacking

CodeIgniter manages its own sessions differently from PHP/Magento’s session management. When you embed your Magento initialization within a CodeIgniter controller or model, CodeIgniter takes over the session handling before Magento has a chance to initialize its own session.

This means that while Magento is still trying to load and validate its customer session, CodeIgniter has already deleted it (or set it to an empty state). Consequently, when you call isLoggedIn(), it returns false because there’s no valid Magento session remaining for it to check.

Solutions: Handling the Session Overlap

To resolve this issue, we need to ensure that CodeIgniter does not interfere with Magento’s session management. Here are a few strategies you can employ:

  1. Temporarily disable CodeIgniter’s session handling: Before including the Magento library and initializing its session, you can temporarily disable CodeIgniter’s own session management using session_write_close(). After Magento has finished running, you can re-enable it with session_start().

  2. Use a dedicated Magento session: You can configure Magento to use a separate session name (different from CodeIgniter’s default session name). This allows both systems to operate independently without clashing sessions.

  3. Load Magento before CodeIgniter: If your setup allows, you could load the Magento application first and then include the CodeIgniter library within it. This ensures that Magento has priority over the session handling mechanism.

Conclusion: Integrating Magento with CodeIgniter

While integrating Magento into a CodeIgniter application presents some challenges, it is certainly possible with careful management of sessions and execution order.

Remember to always initialize Magento before your CodeIgniter controller or model starts executing. You may need to temporarily disable CodeIgniter’s default session handling during this process.

For more in-depth guides on integrating e-commerce platforms like Magento into custom PHP applications, I recommend exploring the Laravel Company.

Happy coding! Let me know if you have any further questions.

API Customers

Our network