0

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?

Josh Carey
  • 81
  • 1
  • 5
  • 2
    _“We'd like to keep them in the site and let them explore more from the same area”_ – well then offer a link/button “more from this area.” This might be helpful to the user no matter which “way” the arrived at the current post - so no need to hide/remove it in the first place. And a button that only emulates the browser’s built-in back functionality is rather nonsense; I would not use one at all. – CBroe Nov 28 '16 at 16:02
  • 1
    I think you are way over thinking this. A) you are setting yourself up for some very confusing paths and logic and B) this will more then likely frustrate your users and cause them to leave. People do not like not knowing where they are. If they want to dig around in your site let them by giving them easy to understand navigation not tricky back buttons. – nerdlyist Nov 28 '16 at 16:20
  • I understand where you guys are coming from, for sure. However, I'm not the designer on this (or other projects that have a similar design and functionality in mind) and this UI is nice and clean, minimal. Its more of an "app experience" kind of approach that the designers have in mind, where they're opening/closing the post as a "panel" of sorts. Even if that's not the way the browser is actually loading the content. – Josh Carey Nov 28 '16 at 21:54

2 Answers2

0

You are setting the variable in your if statement, not checking its value.

if ($previousPage = $url)

should be:

if ($previousPage == $url)
fredrover
  • 2,997
  • 2
  • 17
  • 24
  • Ah, good catch. I fixed that up, but it still didn't fix my issue. But it did get me part of the way... I needed to add an extra step in the URL variable definition. $previousPage was returning: hillsiderancho.com - whereas - $url was returning http://hillsiderancho.com. So it wasn't ever "equal", thus always running the "else" condition. – Josh Carey Nov 28 '16 at 22:30
0

Here's the code that ended up working, after getting my variable corrected (thanks Robyn Overstreet)

  <?php
  $previousPage = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
  $url = site_url();
  $url = preg_replace('#^https?://#', '', $url);

  if ($previousPage == $url){ ?>
     <a href="javascript:history.back();" class="secondaryBtn">
        <div class="inner vAlign">
           <span class="text">BACK</span>
        </div>
     </a>

  <?php } else { ?>
     <a href="/care-ministries/" class="secondaryBtn">
        <div class="inner vAlign">
           <span class="text">Close</span>
        </div>
     </a>
  <?php } ?>

Also, I found the bit I needed to fix the URL (and get it to match up) here: https://stackoverflow.com/a/9549893/4842348

And finally, I should note that my "if" statement href was pointing back to the site homepage (duh, thats what it was told to do) - so I swapped in a basic JS history.back() - that works just fine.

Community
  • 1
  • 1
Josh Carey
  • 81
  • 1
  • 5