1

This code is not displaying the referrer URL of the website from which the user has come to the current page. Instead, it is displaying the website base URL any idea why it is happening? It is working fine on my website. when the user moves from product to contact page etc. I also tried this too.

echo $_SERVER['HTTP_REFERER'];

but not working.

Here is my current code

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $redirect = $objectManager->create('\Magento\Framework\App\Response\RedirectInterface');
             echo $redirect->getRefererUrl();

Output: http://www.yourwebsite.com/

Expected Result: http://www.previouswebsite.com/

0

In PHP you can call simply by, $_SERVER[“HTTP_REFERER”], In Magento 2 Best Practice, you need to call RedirectInterface in your __construct() function and get referrer URL.

public function __construct(
    \Magento\Framework\App\Response\RedirectInterface $redirect
) {
    $this->redirect = $redirect;
}

Call below the line in your function,

echo $this->redirect->getRefererUrl();
echo $this->redirect->getRedirectUrl();

copied from RAKESH JESADIYA blog

| improve this answer | |
0

I had the same question and found several answers that use \Magento\Framework\App\Response\RedirectInterface.

At least as of 2.3.4, using the getRefererUrl() and getRedirectUrl functions of this class does not reliably return the same value as $_SERVER['HTTP_REFERER']

I grepped the vendor/framework folder for "HTTP_REFERER" and found that \Magento\Framework\HTTP\Header includes the function:

/**
 * Retrieve HTTP REFERER
 *
 * @param boolean $clean clean non UTF-8 characters
 * @return string
 */
public function getHttpReferer($clean = true)
{
    return $this->_getHttpCleanValue('HTTP_REFERER', $clean);
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.