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 gkornbluth - November 2, 2015 - edited: November 2, 2015

Hi geminiman7,

Not sure exactly what you're asking for that's not in the code already included in the sample login template.

This code is at the top of the viewer after the  'load viewer library call

   // error checking
  $errorsAndAlerts = alert();
 if (@$CURRENT_USER  ) { $errorsAndAlerts .= "You are already logged in!<br /><br /><a class='special-link' href='{$GLOBALS['WEBSITE_LOGIN_POST_LOGIN_URL']}'>Click/Tap here to continue</a><br /> or<br /><a class='special-link' href='?action=logoff'>Click/Tap here to Log Off</a><br/>\n"; }
 if (!$CURRENT_USER && @$_REQUEST['loginRequired']) { $errorsAndAlerts .= "Please login to continue.<br/>\n"; }

   // save url of referring page so we can redirect user there after login
  if (!getPrefixedCookie('lastUrl')) { setPrefixedCookie('lastUrl', @$_SERVER['HTTP_REFERER'] ); }

$GLOBALS['WEBSITE_LOGIN_POST_LOGIN_URL is one of the variables that you can change near the top of the websiteMembership.php plugin. Make sure that you make this a relative URL I.E.: $GLOBALS['WEBSITE_LOGIN_LOGIN_FORM_URL']  = '/your_page_name.php';  and not an absolute URL .IE.: $GLOBALS['WEBSITE_LOGIN_LOGIN_FORM_URL']  = http://www.your_page_name.php';  or you may get confusing redirect results.

This code is in the body of the login template to display the $errorsAndAlerts

<?php if (@$errorsAndAlerts): ?>
    <div style="color: #C00; font-weight: bold; font-size: 13px;">
      <?php echo $errorsAndAlerts; ?><br/>
  <?php endif ?>

You can also put the code below on any viewer  that requires login for access (at the top of the viewer after the  'load viewer library call), which will automatically redirect the member to the login form

<?php  if (!$CURRENT_USER) { websiteLogin_redirectToLogin(); } ?>

There's a lot of useful, well documented code in the website membership plugin code generator on the plugins page.

Hope that gets you started


Jerry Kornbluth

The first CMS Builder reference book is now available on-line!

Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

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.