Forum

Thread tagged as: Question

Two sign out buttons?

Hi,

For some reason, I have two sign out buttons! This is in my Global Header template and the relevant code I am using is as follows. I have tried to use a php 'if' to display a 'Sign In' button whilst the user isn't signed in. And then display a 'Sign Out' button when the user is signed in.

<?php
  $cart_count = perch_shop_cart_item_count([], true);
  $cart_classes = 'cart';
  if($cart_count > 0)
  {
      $cart_classes .= ' has-items';
  }

  if(perch_member_logged_in())
  {
      $account_link_name = 'Sign out';
  }
  else
  {
      $account_link_name = 'Sign in';
  }
?>
<nav class="navbar is-transparent">
      <div class="navbar-brand">
        <a class="navbar-item" href="/">
          <img src="/assets/img/Sandford-Parks-Lido-Cheltenham.png" alt="Sandford Parks Lido is one of the largest outdoor swimming pools in the UK, set in landscaped gardens in Cheltenham, England." width="130" height="51">
        </a>
        <div class="navbar-burger burger" data-target="navbarExampleTransparentExample">
          <span></span>
          <span></span>
          <span></span>
        </div>
      </div>

      <div id="navbarExampleTransparentExample" class="navbar-menu">
        <div class="navbar-start">
          <div class="navbar-item has-dropdown is-hoverable">
            <a class="navbar-link" href="/">
              Shop
            </a>
            <div class="navbar-dropdown is-boxed">
              <a class="navbar-item" href="/photo-upload">
                Upload photo
              </a>
              <a class="navbar-item" href="/shop/checkout-stage-one">
                Required concession documentation
              </a>
              <a class="navbar-item" href="/concession-application">
                Apply for concessions
              </a>
            </div>
          </div>
          <a class="navbar-item" href="https://www.sandfordparkslido.org.uk/">
            Return to main site
          </a>
        </div>

        <div class="navbar-end">
          <div class="navbar-item">
            <div class="field is-grouped">
              <p class="control">
                <a class="button is-primary" href="/checkout" >
                  <span class="icon is-small"><i class="fa fa-user-circle-o" aria-hidden="true"></i></span>
                  <span><?php echo $account_link_name; ?></span>
                </a>
              </p>
              <?php if(perch_member_logged_in()): ?>
              <p class="control">
                <a class="button is-primary" href="/shop/logout">
                  <span class="icon is-small"><i class="fa fa-user-circle-o" aria-hidden="true"></i></span>
                  <span><?php echo $account_link_name; ?></span>
                </a>
              </p>
              <?php endif; ?>

So the relevant bit of code that isn't producing the results I intended is:

<?php if(perch_member_logged_in()): ?>

Would appreciate a bit of help...

Grant Smith

Grant Smith 0 points

  • 3 years ago
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Grant,

It seems you are echoing $account_link_name twice. Once before the if statement and once inside it.

<div class="navbar-item">
<div class="field is-grouped">
<p class="control">
<a class="button is-primary" href="/checkout" >
<span class="icon is-small"><i class="fa fa-user-circle-o" aria-hidden="true"></i></span>

<!-- this is the first one -->
<span><?php echo $account_link_name; ?></span>
</a>
</p>

<!-- the second one is inside the if statement  -->
<?php if(perch_member_logged_in()): ?>
<p class="control">
<a class="button is-primary" href="/shop/logout">
<span class="icon is-small"><i class="fa fa-user-circle-o" aria-hidden="true"></i></span>
<span><?php echo $account_link_name; ?></span>
</a>
</p>
<?php endif; ?>

Thank for this Hussein, although I do not disagree with your post, I am still struggling to get this work as expected?

Essentially the Sign in remains although the user is logged in.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Have you tried removing the one you don't want?

So here is what I have tried…

<div class="field is-grouped">
  <p class="control">
    <a class="button is-primary" href="/checkout" >
      <span class="icon is-small"><i class="fa fa-user-circle-o" aria-hidden="true"></i></span>
      <span>Sign in</span>
    </a>
  </p>
  <?php if(perch_member_logged_in()): ?>
  <p class="control">
    <a class="button is-primary" href="/shop/logout">
      <span class="icon is-small"><i class="fa fa-user-circle-o" aria-hidden="true"></i></span>
      <span><?php echo $account_link_name; ?></span>
    </a>
  </p>
  <?php endif; ?>

Result: Sign in and Sign out button once logged in

<div class="field is-grouped">
  <p class="control">
    <a class="button is-primary" href="/checkout" >
      <span class="icon is-small"><i class="fa fa-user-circle-o" aria-hidden="true"></i></span>
      <span><?php echo $account_link_name; ?></span>
    </a>
  </p>
  <?php if(perch_member_logged_in()): ?>
  <p class="control">
    <a class="button is-primary" href="/shop/logout">
      <span class="icon is-small"><i class="fa fa-user-circle-o" aria-hidden="true"></i></span>
      <span>Sign out</span>
    </a>
  </p>
  <?php endif; ?>

Result: Two Sign out buttons once logged in

<div class="field is-grouped">
  <?php if(perch_member_logged_in()): ?>
  <p class="control">
    <a class="button is-primary" href="/shop/logout">
      <span class="icon is-small"><i class="fa fa-user-circle-o" aria-hidden="true"></i></span>
      <span><?php echo $account_link_name; ?></span>
    </a>
  </p>
  <?php endif; ?>

Result: No Sign in button

<div class="field is-grouped">
  <p class="control">
    <a class="button is-primary" href="/checkout" >
      <span class="icon is-small"><i class="fa fa-user-circle-o" aria-hidden="true"></i></span>
      <span><?php echo $account_link_name; ?></span>
    </a>
  </p>

Result: One button, but can not log out once logged in

Drew McLellan

Drew McLellan 2638 points
Perch Support

What does this give you?

<?php if(perch_member_logged_in()) { ?>
    logged in
<?php } else { ?>
    not logged in
<?php } ?>

It gives me exactly what I am after!!!

Thanks very much, appreciate the help