magento - including php file in the root folder

magento - including php file in the root folder

September 26, 2026 · Illustrated author avatarBy Stefan Bogdanescu
magento - including php file in the root folder

Persisting Magento Session Variables Across Custom PHP Files

Understanding the Problem

In your Magento installation, you’ve created an external PHP file other.php located in the root folder for handling form submission from a product page. The session variable $_SESSION['myGroupID'] is successfully set within this script, but it fails to persist when the user navigates back to the referring Magento page.

The issue arises because you’re stepping outside of Magento’s core framework to handle the request in your own PHP file. This breaks the built-in session management and storage mechanisms that Magento provides.

Why Sessions Aren’t Persisting

When you use $_SESSION directly, it operates independently of the main application context. In a standard LAMP stack, sessions are stored server-side using the session.save_path setting in your PHP configuration file (php.ini).

However, Magento uses its own session handling mechanisms for its core functionality and third-party extensions. These include the /var/session directory and the Mage_Core_Model_Session class.

When you access $_SESSION directly outside of the Magento context (like in your other.php file), it creates a separate session namespace that isn’t linked to Magento’s internal session management system.

The Solution: Integrating Custom PHP with Magento Sessions

To ensure session persistence across your custom PHP files and the main Magento application, you need to integrate these two environments. Here are the steps:

1. Include Magento’s Initialization File

At the top of your other.php file, include Magento’s bootstrap file using the following code:

// Include the Magento bootstrap file
require_once 'app/Mage.php';

This pulls in the core Magento application context.

2. Use Magento’s Session Handling Class

Replace the direct use of $_SESSION['myGroupID'] with Magento’s Mage::getSingleton('core/session'):

// Set a session variable using Magento's class
$session = Mage::getSingleton('core/session');
$session->setMyGroupId($yourValue);

3. Redirect Back to the Main Application

When redirecting, use Mage::helper('core/url')->getUrl() to generate the URL:

// Redirect back to the referring page using Magento's helper
header("Location: " . Mage::helper('core/url')->getUrl($referrer));
exit();

4. Restarting the Session in Your Custom PHP File

Ensure you’re restarting the session within your custom PHP file by adding session_start() before any session-related operations:

<?php
// Start the session
session_start();

// Include Magento's bootstrap file
require_once 'app/Mage.php';

// Set a session variable using Magento's class
$session = Mage::getSingleton('core/session');
$session->setMyGroupId($yourValue);

// Redirect back to the referring page using Magento's helper
header("Location: " . Mage::helper('core/url')->getUrl($referrer));
exit();
?>

Conclusion

Persisting session variables when stepping outside of the Magento framework requires careful integration of your custom PHP scripts with Magento’s core application. By including the bootstrap file, using Magento’s session handling class, and redirecting back to the main application context, you can maintain the continuity of sessions across both environments.

Remember, while this approach solves the immediate problem, it’s essential to consider whether your requirements could be met within the Magento framework itself, as custom PHP files can introduce maintenance overhead and potential security vulnerabilities. Always strive to leverage Magento’s built-in functionality where possible.

For further reading on Magento development best practices, I recommend exploring our comprehensive guides at Laravel Company.

Catalog Extensions Configuration Errors

Our network