Determine if user is logged in

Permalink 3 users found helpful
I was looking through the API to see if there is a way to test to see if a user is logged in - but I couldn't come up with anything. does anyone know of a way to determine this? I would like to have a "Log In" link if the user isn't alreay logged in, but if they are, I want a "Log Out" link. Any help would be appreciated.

Thanks

 
ScottC replied on at Permalink Reply
ScottC
Check in there :)
isLoggedIn()
chrisalbrecht replied on at Permalink Reply
exactly what I was after ;)

Cheers
hockeyshooter replied on at Permalink Reply
hockeyshooter
If I inspect isLoggedIn before calling $u = new User(); I get an undefined function error; if I call $u = new User(); first, I get a whole bunch of errors and isLoggedIn still doesn't work. Am I missing another call first?
elyon replied on at Permalink Reply
elyon
global $u;
if ($u -> isLoggedIn ()) {
echo "Logged in!";
}
bryanlewis replied on at Permalink Reply
bryanlewis
What file do I add this code too?
frz replied on at Permalink Reply
frz
in a theme, in any template when you want to know if a user is loggd in for presentation purposes...
bryanlewis replied on at Permalink Reply
bryanlewis
it says that I'm logged in but is there a logout button?
hockeyshooter replied on at Permalink Reply
hockeyshooter
The logout button is always at the right-hand end of the button bar at the top (theme-dependant, I guess). If you want to make your own logout button, just copy its URL.
bryanlewis replied on at Permalink Reply
bryanlewis
Don't I have to put some type of code in there to actually log the user out?
hockeyshooter replied on at Permalink Reply
hockeyshooter
The script that URL takes you to does all the work.
bryanlewis replied on at Permalink Reply
bryanlewis
that script just tells me i'm logged in? sorry i'm a bit confused...
hockeyshooter replied on at Permalink Reply
hockeyshooter
I was assuming you were after a way for a user to log out - I was suggesting you provide a button or link to:
/index.php/login/-/logout
bryanlewis replied on at Permalink Reply
bryanlewis
that worked great but do you know how to let that button only show up when i'm logged in?
bryanlewis replied on at Permalink Reply
bryanlewis
this just throws an error (I'm a beginner :( )

<?php
global $u;
if ($u -> isLoggedIn ()) {
echo "<a href="http://www.sitename.com/index.php/login/-/logout"><input type="button" name="button1" value="logout" /></a>";
}
?>
ScottC replied on at Permalink Reply
ScottC
<? $uinfo = new User();
if($uinfo->IsLoggedIn()){
echo('<a href="http://client.turnpostadmin.com/index.php/login/-/logout"><input type="button" name="button1" value="logout" /></a>');
} ?>

Global $u will usually work, but a new user object will always work :)

also i always wrap echo in the parenthesis (habit) and single quotes are your friend.
bryanlewis replied on at Permalink Reply
bryanlewis
thanks a million!!!
synlag replied on at Permalink Reply
synlag
<?php
            $uinfo = new User();
          if($uinfo->IsLoggedIn()){ ?>
            <a href="<?php echo DIR_REL?>/index.php/login/-/logout">Logout</a> | <a href="<?php echo $this->url('/profile')?>">My Profile</a>
        <?php   
         } else { ?>
           <a href="<?php echo $this->url('/login')?>">Login</a> | <a href="<?php echo $this->url('/register')?>">Register</a>
        <?php   }?>
frz replied on at Permalink Reply
frz
in the interest of turning bryan into a bit more of a developer - you were almost there your quotes were ending the variable field in PHP too early... scott just wrapped your double quotes in single quotes so PHP would know to spit the whole thing out..


something about horses and water..
larsnieuwenhuizen replied on at Permalink Reply
larsnieuwenhuizen
Thanks, was looking for this! :)
chris123uk replied on at Permalink Reply
chris123uk
thought this might help someone out there.

its for core commerce add on.
shows different stuff if admin is logged in:

<div class="login">
      <?php
      global $u;
      $u = new User();                                       
      global $cp;
      $canViewToolbar = (isset($cp) && ($cp->canWrite() || $cp->canAddSubContent() || $cp->canAdminPage() || $cp->canApproveCollection()));
      if ($canViewToolbar) {                                          
         $userName = '<br><a class="removeBtnStyle" href="' . $this->url('/index.php/dashboard/core_commerce/orders/search/') . '">Click to view orders</a>';               
         ?>
         <p class="loggedInAdmin"><?php echo 'Currently logged in as '; echo $u->getUserName().'. '; echo $userName;?> <a style="margin-left: 10px;" href="<?php echo $this->url('/login', 'logout')?>"><?php echo (' Sign Out')?></a></p>
   <?php   }
      elseif (!$u->getUserName()) { 
         ?>
         <p class="notLoggedIn">You are not logged in<a href="<?php echo $this->url('/login')?>"><?php echo (' Sign In')?></a></p>
         <?php