Jump to content
TNG Community
Philip Roy

Links in top menu based on login and logout status

Recommended Posts

Philip Roy

I'm using template 18 and at the top of the front page, I want a link that says "Log In". When the user clicks that and logs in, I want the text to change to 2 links..."Most Wanted" and "Log Out"

The great news is that on the front page, I have figured out the code. I have added this code to /templates/template18/index.php

Quote

<?php
    if( $currentuser ) {
        echo "<li><a href=\"mostwanted.php\">{$text['mostwanted']}</a></li><li><a href=\"logout.php\">{$text['mnulogout']}</a></li>";
    }
    else {
        echo "<li><a href=\"login.php\">{$text['mnulogon']}</a></li>";
        }
?>

So I got that going and it works perfectly on the main page of the site. But when you move away from the main page, that all disappears. I realised that I need to edit /templates/template18/topmenu.php also for the rest of the sites pages.

But this is where things start to go weird. Inserting the same code into the top menu file, it works in the exact opposite way to what you expect...it suggests you log in, when you already are. It's not picking up that currentuser variable. If I try using !$currentuser (and I'm not sure why I would) it also kind of works.

On top of that, I have some histories pages that are available without login in, and it seems to ignore the fact that those don't require login.

My site is a private site and still under development, so I can't share the link. All users are required to login to view any content, except a few history pages that I mentioned.

TNG v12.2

Thanks,

Phil

Share this post


Link to post
Share on other sites
Philip Roy

Ok, posting this for others....there's 3 parts to solving this.

1. You need to assign the user to a Tree and person ID when setting up the user (this was my mistake in my test site that was causing the issue)

2. Change line 2 of topmenu.php from...

global $text, $cms, $subroot, $tmp; 

...to...

global $text, $currentuser, $cms, $allow_admin, $subroot, $tmp;

3. The code to have what I wanted appearing ("Most Wanted" and "Log Out" when they are logged in, and "Log In" only. when they aren't) is as follows...

<?php
    	if ($_SESSION['mypersonID'] != "" && $currentuser && $_SESSION['mygedcom'] != "") {
        echo "<li><a href=\"mostwanted.php\">{$text['mostwanted']}</a></li><li><a href=\"logout.php\">{$text['mnulogout']}</a></li>";
    }
    else {
        echo "<li><a href=\"login.php\">{$text['mnulogon']}</a></li>";
        }
?>

Cheers,

Phil

Share this post


Link to post
Share on other sites
Philip Roy

I spoke too soon....I'm finding the path of the website doesn't always work properly, so I have just hard coded that in...

<?php
    	if ($_SESSION['mypersonID'] != "" && $currentuser && $_SESSION['mygedcom'] != "") {
        echo "<li><a href=\"https://www.website.com/mostwanted.php\">{$text['mostwanted']}</a></li>
		<li><a href=\"https://www.website.com/logout.php\">{$text['mnulogout']}</a></li>";
    }
    else {
        echo "<li><a href=\"https://www.website.com/login.php\">{$text['mnulogon']}</a></li>";
        }
?>

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×