Referer URL is the address of the webpage where a person clicked a link that sent them to your page. Referer is the webpage that the visitor was on before landing on your page.
When a non-logged in user tries to add the product to the wishlist, the default Magento 2 generates one referer URL and redirects that user to the login page. For example, if you write customer/account after your store URL without logging in, Magento 2 automatically redirects you to the login page using the referer URL.
It’s the default behaviour of the Magento 2, but what if you want the same concept in other pages and functionalities on your store also?
You can create referer URL in Magento 2 to implement the below functionalities in your store.
- Restrict user to access order page directly without login.
- Direct URL access restriction without logging in.
- Restrict payments using a specific method for non-logged-in users.
Follow the below solution and restrict the user to access your pages directly without login and get rid of unauthenticated users.
Method to Create Referer URL in Magento 2
class Index extends Action\Action
public function __construct(Action\Context $context, UrlInterface $urlInterface)
$this->urlInterface = $urlInterface;
public function execute()
$url = $this->_redirect->getRefererUrl();
$login_url = $this->urlInterface
array('referer' => base64_encode($url))
$resultRedirect = $this->resultRedirectFactory->create();
You are always welcome to mention your doubts regarding this solution in the Comments section below.
I would be glad to help you.
Feel free to share this solution with Magento Community via social media.
Get Weekly Updates
Never miss Magento tips, tricks, tutorials, and news.
Thank you for subscribing.
Something went wrong.