Resolving Magento 1.3.2.2 Fatal Error: Call to member function getUsername() on a non-object
If you’re facing a Fatal error in your Magento 1.3.2.2 installation when confirming an order, you’ve likely seen this message:
Fatal error: Call to a member function getUsername() on a non-object
in /path/to/your/magento/app/design/adminhtml/default/default/template/page/header.phtml on line 31
This issue occurs because the session object is not being properly initialized. Let’s dive into the root cause and how to resolve it.
Understanding the Problem
The error arises in the admin header file (header.phtml) when trying to access a property of the session object (getUsername()). This happens due to a timing issue where the Mage_Core_Model_Session is not fully initialized before being accessed. Magento 1.x has some quirks with session management that can lead to this kind of problem.
Temporary Workaround: Refresh the Page
As mentioned in your question, refreshing the page after the error usually works around it and redirects you to the order success page as expected. This is because the second request properly initializes the session object before it’s accessed.
However, relying on a manual refresh isn’t a sustainable solution for either customers or admins using your store. Let’s find a permanent fix.
Permanent Solution: Update Core Files
To resolve this issue permanently, you’ll need to update two core files in Magento:
- app/code/core/Mage/Core/Model/Session.php
Open Session.php and locate the start() method (around line 140). In the start() function, add these lines just before the $this->getCookieManager()->setReadOnly(true); line:
if (!$this->isSessionStarted()) {
$this->start();
}
This ensures that the session is always started before it’s accessed.
- app/design/adminhtml/default/default/template/page/header.phtml
In header.phtml, find the following lines (usually around line 31):
$username = Mage::getSingleton('admin/session')->getUsername();
Replace them with:
if (Mage::getSingleton('admin/session') !== null) {
$username = Mage::getSingleton('admin/session')->getUsername();
} else {
$username = '';
}
This checks if the session object exists before trying to access its properties, preventing the fatal error.
Reindex and Cache Management
After updating these files:
- Run a reindex in your Magento backend under System > Index Management.
- Clear your cache by going to System > Cache Management and clicking “Flush Magent Cache”.
Final Thoughts: Upgrading to a Supported Version
While the above solution should resolve your immediate issue, it’s crucial to understand that Magento 1.x is no longer supported as of June 2020. This means you won’t receive security patches for vulnerabilities found in these older versions.
We strongly recommend upgrading to Magento 2 or migrating to a modern e-commerce platform like Laravel-based e-commerce solutions (which we specialize in at Laravel Company).
Magento 2 has improved session management and doesn’t suffer from this particular issue. It also offers better performance, security features, and compatibility with modern technologies.
Conclusion
In conclusion, to resolve the getUsername() fatal error in Magento 1.3.2.2:
- Update
Session.phpin the Mage_Core_Model directory. - Update the admin header template file (
header.phtml) to check for the session object before accessing its properties. - Reindex and clear your cache after updating.
While this fixes the immediate issue, remember that long-term maintenance of Magento 1.x is no longer supported. Consider upgrading or migrating to a more modern platform for better stability and security in the future.