Difficulties programming simple login/logout script (Website member plugin)

By geminiman7 - October 30, 2015


I'm trying to program a PHP if/then script that displays a "Logout" link if a user is already logged in, OR if they're not logged in, it should just say "Login".

Although I've been using CMSB for years, I'm not very knowledgeable about PHP coding so having a very difficult time getting this to work. Can someone please help me complete the code to accomplish this? The script is something along the line of:

if $@CURRNENT_USER = login then output Logout =?action=logoff
elseif CURRENT_USER = logout then output Login=action=login

I know the two lines of code are all wrong LOL, but you get what I'm trying to achieve. I'm just having a rough time understanding the logic and searching the Internet isn't helping!

Greatly appreciated..

By geminiman7 - November 4, 2015

Thanks for your reply Jerry. I already did review the sample code from the code generator pages. None of those pages do what I am trying to achieve.

Quite simply, at the top right of every page of the website, I want it to display "Login" hyperlink if a user is NOT logged in, and if a user IS logged in, the hyperlink should say "Logout" instead.

The sample code provided is programmed to only display an error message if you are already logged in and try to login again (THEN it gives you the option to logout, but only from the login page). But that's not what I want to accomplish.

I know there is a way to accomplish this via a PHP IF/THEN statement, but again, I'm having an incredibly difficult time understanding the logic and how to correctly write the code. Does it make more sense now?

By geminiman7 - November 4, 2015

I just noticed, a PERFECT example is the interactivetools.com website. Notice near the upper right of the page, it says "Login" if you're not logged in, or "Logoff (username)" if you are logged in. That's what I am trying to accomplish...

By geminiman7 - November 4, 2015

Wow, I have NO idea how I did this, but I managed to get it to work with this code:

<?php if (!@$CURRENT_USER): ?>
<?php echo ("<a href='/directory/'>Login</a>"); ?>
<?php else: ?>
<?php echo ("<a href='?action=logoff'>Logoff</a>"); ?>
<?php endif ?>

I was just playing around and didn't expect this to work. I still don't understand the logic of what's happening here in the code LOL. I would like to understand this better. Can you shed a little light on how this is working?

By Damon - November 5, 2015


Here are the details about how this code is working:

<?php if (!@$CURRENT_USER): ?>
<?php echo ("<a href='/directory/'>Login</a>"); ?>
<?php else: ?>
<?php echo ("<a href='?action=logoff'>Logoff</a>"); ?>

The 1st line is saying if the CURRENT_USER doesn't exist (no one is logged in) then do the following.

The 2nd line prints a Login link to the screen.

The 3rd link gives the alternative which is, a CURRENT_USER does exist and so go to line 4th libe which prints out the Logoff link.

Hope this helps.

Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.