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

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

<?php
   // 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/>
    </div>
  <?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

Best,

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

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?

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...

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

Hi,

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.

Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/