get title and url of the previous page on Magento

get title and url of the previous page on Magento

August 17, 2026 · Illustrated author avatarBy Stefan Bogdanescu
get title and url of the previous page on Magento

Retrieving Previous Page Title and URL in Magento

As an e-commerce developer, you’re often tasked with tracking user behavior on your Magento site. One common requirement is to capture the title and URL of the previous page that customers visited before arriving at their current page. This data can be invaluable for analyzing customer flow, identifying popular product categories, and optimizing navigation.

Getting the Previous Page URL

The first step in retrieving this information is to access the HTTP_REFERER server variable, which stores the URL of the page from which a user came to your site. Here’s how you can do it:

$previousPageUrl = $_SERVER['HTTP_REFERER'];

This line will give you the full URL of the previous page. However, be aware that not all browsers send this information by default, and some users may have configured their settings to prevent its transmission.

Retrieving the Previous Page Title

Getting the title is more challenging because it’s not directly available in Magento. You’ll need to parse the HTML content of the previous URL to extract the title tag. Here’s a function that does exactly this:

function getPreviousPageTitle($url)
{
    $content = file_get_contents($url);
    $dom = new \DOMDocument;
    @$dom->loadHTML($content);

    return $dom->getElementsByTagName('title')->item(0)->nodeValue;
}

This function uses the DOM extension to load the HTML content of the given URL and then retrieves the value of the first <title> tag found.

Using a Custom Observer for Better Control

To handle cases where HTTP_REFERER is not available, you can create a custom observer for the controller_action_postdispatch event. This allows you to track the previous route and store it in the session:

public function controllerActionPostdispatch($observer)
{
    $action = $observer->getEvent()->getControllerAction();
    $request = $action->getRequest();

    if ($previousRoute = $request->getParam('___from_route')) {
        Mage::getSingleton('customer/session')->setPreviousRoute($previousRoute);
    }
}

You can then retrieve the previous route from the session and redirect to it:

$previousRoute = Mage::getSingleton('customer/session')->getPreviousRoute();
if ($previousRoute) {
    $this->_redirectUrl(Mage::helper('url')->getUrl($previousRoute));
}

Displaying the Previous Page Information

Once you have retrieved the title and URL, you can display them on your current page using Magento’s layout XML:

<reference name="content">
    <block type="core/text" template="your_module/previous_page.phtml"/>
</reference>

In your previous_page.phtml template file, you can use the following code to output the title and URL:

<?php if ($previousPageUrl = Mage::getSingleton('customer/session')->getPreviousPageUrl()): ?>
    <p>You came from: <?= $this->escapeHtml($previousPageUrl) ?></p>
    <?php if ($previousPageTitle = Mage::getSingleton('customer/session')->getPreviousPageTitle()): ?>
        <p>The title of that page was: <?= $this->escapeHtml($previousPageTitle) ?></p>
    <?php endif; ?>
<?php endif; ?>

Conclusion

Retrieving the previous page’s URL and title in Magento requires a combination of server variables, HTML parsing, and custom events. By understanding how to track this information effectively, you can gain valuable insights into your customers’ navigation behavior and improve the overall user experience on your store.

Remember that user privacy is crucial when handling referrer data. Always ensure that you comply with relevant laws and guidelines, such as GDPR, when collecting and processing this type of customer data.

For a deeper dive into Magento development best practices and tutorials, be sure to check out our comprehensive guides at Laravel Company.

Catalog Performance Extensions Configuration

Our network