Don't show a button on a logged in users profile

I have a follow/unfollow system, logged in users can browse profiles and choose to follow/unfollow other profiles across the site.

There are two buttons - a follow and an unfollow - the user will see one or the other, depending on whether the user is already following the profile they are on.

These buttons are not shown if the person browsing is not logged in (ie if the $_SESSION['user'] is not set/empty. However the follow button still will appear on the user’s own profile page but I don’t want it to, I don’t want users to be able to follow themselves.

I’m stuck on what I need to do to add an additional statement which will hide the follow button if the $_SESSION['user']['id'] matches with the profile they are looking at.

The code I’m using is;

  <?php if(empty($_SESSION['user'])) { ?>
  <?php } else { ?>
    <?php if (check_follower_status($_GET['uid'],$_SESSION['user']['id']) == 0) { ?>
      <a class="uibutton large pull-right" href="includes/process.php?do=follow&uid=<?php echo $_GET['uid']; ?>">+ Follow</a>
      <?php } else { ?>
      <a class="uibutton large pull-right" href="includes/process.php?do=unfollow&uid=<?php echo $_GET['uid']; ?>">- Unfollow</a>
    <?php } ?>
  <?php } ?>

An example profile URL is - profile.php?username=Test1&uid=19 - so the current profile user id is 19 in tis case.

Maybe take more of the logic out of the html bit and decide this before-hand.
Rough example

$showButton = NULL; // off by default

if($_SESSION['user']['id'] !== $profileID) { // Not your profile
    if (check_follower_status($_GET['uid'],$_SESSION['user']['id']) == 0) { // Button is Follow
        $showButton = '<a class="uibutton large pull-right" href="includes/process.php?do=follow&uid='. $_GET['uid'] .'">+ Follow</a>';
    }
    else{  // Button is Unfollow
        $showButton  = '<a class="uibutton large pull-right" href="includes/process.php?do=unfollow&uid='. $_GET['uid'] .'">- Unfollow</a>';
    }
}

Then in the html just:-

<?php if($showButton) { echo $showButton ;} ?>
1 Like

Thanks. Yes, that does make more sense.

I am getting a syntax error with your code though; Parse error: syntax error, unexpected 'else' (T_ELSE).

I missed the closing bracket } after the if condition. I did say it was a rough example. :smile:
Looking again, it is there, in the wrong place after the else.

I have edited the post to correct that now.

Other things I may do to improve it would be to put the $_GET['uid'] into a “clean” variable to use in the code.

$profileID = preg_replace('#[^0-9]#', '', $_GET['uid']);

That will help avoid any code injection in places like here:-

$showButton = '<a class="uibutton large pull-right" href="includes/process.php?do=follow&uid='. $_GET['uid'] .'">+ Follow</a>';

Instead you would than have:-

$showButton = '<a class="uibutton large pull-right" href="includes/process.php?do=follow&uid='. $profileID .'">+ Follow</a>';

Which you know can only be an integer now.

1 Like

Perfect - thanks @SamA74 for the help :thumbsup:

1 Like

If I was to use ajax to submit the follow/unfollow buttons to do away with the need to reload the page how would I work that into the if statement - I’ve tried using ajax already on this but because the page hasn’t reloaded, the query hasn’t ran again either meaning it still shows the follow button when they have just been followed - it only shows unfollow when you come back to the page.

Is there a way to use ajax here that will allow the correct buton to display?

Wouldn’t you need to use the success() function on return from your Ajax call to identify the button for the user that you just followed, and replace the button text with the appropriate wording? And possibly replace the html with the change in parameter there, too.

Is there a reason for specifying “follow” or “unfollow” in the link? That is, could you not rely on checking whether the user is already following the other user, and just invert the current status if it’s called again?

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.