I've tried to find a solution to this a few different times, but keep coming up empty on a working solution. Essentially, I'm trying to find a way to have a link that is either a "close" or a "back" link in functionality. The condition being IF the use just came from another page within the same site OR if the user came into this page from an external link (search engine or otherwise).
Here's a couple links for further explanation and examples of how its NOT working: This link shows a "list" or archive of posts: http://hillsiderancho.com/care-ministries/
Once a user goes into one of those posts (http://hillsiderancho.com/care-ministries/celebrate-recovery/), the link in the top right should read "Back", and should simply act as the browser back button, to allow the user to go back to viewing the list of pages (or possibly some other page that lead them to this post).
The other use case would be if the user comes into the post from an external site, the button should read "Close" and then point the user to the list page, not a "browser back" function that takes them away from the site again. We'd like to keep them in the site and let them explore more from the same area.
This is the code I have in place, and the "Back" button has a different functionality in nearly all of the different posts in this section... seeming as though its never hitting the "else" condition, and only coming up with some other strange referrer URL. (I just noticed a typo on my href in the "else" condition, but either way, its not getting to that page either):
<div class="post-close-btn">
<?php
$previousPage = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
$url = site_url();
if ($previousPage = $url){ ?>
<a href="<?php echo $_SERVER['HTTP_REFERER']; ?>" class="primaryBtn">
<div class="inner vAlign">
<span class="text">BACK</span>
</div>
</a>
<?php } else { ?>
<a href="/care-ministries/" class="primaryBtn">
<div class="inner vAlign">
<span class="text">Close</span>
</div>
</a>
<?php } ?>
Can anyone help with this?