Alaa
Alaa

Reputation: 21

How to make a back button using http_referer in Smarty template for internal links only

I am using the following code on my website to make a Back button in Smarty template:

<div id="back-button">
<a href="{$smarty.server.HTTP_REFERER}">BACK</a>

This works as needed when navigating through internal pages. But, when a user visits my website via a hyperlink on external website, the back button redirects him to that external site. I want the button to work only if http_referer value is an internal link. As for the external links, it should redirect to the homepage of my website.

Sorry if you my question is extremely easy. I am new to Smarty.

P.S. I know how to make it in Javascript. I need it using smarty.

Thanks

Upvotes: 1

Views: 2191

Answers (1)

Alexander Grosul
Alexander Grosul

Reputation: 1814

Try this

{if isset($smarty.server.HTTP_REFERER) && strpos($smarty.server.HTTP_REFERER, $smarty.server.HTTP_HOST) != false}
  <a href="{$smarty.server.HTTP_REFERER}">Back</a>
{else}
  <a href="/">Back</a>
{/if}

it checks if a referer exists and is from the same host and then add a referrer link if not it add a homepage link

Upvotes: 4

Related Questions