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
Thanks
Check in there :)
exactly what I was after ;)
Cheers
Cheers
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?
What file do I add this code too?
in a theme, in any template when you want to know if a user is loggd in for presentation purposes...
it says that I'm logged in but is there a logout button?
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.
Don't I have to put some type of code in there to actually log the user out?
The script that URL takes you to does all the work.
that script just tells me i'm logged in? sorry i'm a bit confused...
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
/index.php/login/-/logout
that worked great but do you know how to let that button only show up when i'm logged in?
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>";
}
?>
<?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>";
}
?>
<? $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.
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.
thanks a million!!!
<?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 }?>
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..
something about horses and water..
Thanks, was looking for this! :)
thought this might help someone out there.
its for core commerce add on.
shows different stuff if admin is logged in:
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
Viewing 15 lines of 28 lines. View entire code block.