alan

isLoggedin() and isSuperuser() doing nothing (as if commented out) in a PW3 site - did I miss something?

Recommended Posts

Sorry folks if this is obvious to most and I missed something in the docs or here in the forum, but, I have cheerfully used code such as:

if($user->isLoggedin()) {
  echo " loggedIn";
} else {
  echo " notLoggedIn";
}

in PW 2.x sites.

Now on a v3 site I am finding even the most basic test to see if the viewer is logged in or `isSuperuser()` is not giving an error but simply doing nothing - not evaluating.

For example, the top of a typical template reads:

<?php namespace ProcessWire;

			if($user->isLoggedin()) {
				echo " loggedIn";
			} else {
				echo " notLoggedIn";
			}

I have `debug` turned on.

Is there something obvious I have missed, perhaps a step required for v3 that's not required for v2 to allow access to $user data?

Thanks for any pointers.

Share this post


Link to post
Share on other sites

Nothing different from PW2 to 3 really. This code is even in the default install themes and working normally.

  • Like 1

Share this post


Link to post
Share on other sites

Thanks @Soma for confirming that.

I'm confused in that case as I have a site where this test is failing.

I'll go back and re-re-check to see if I am missing something else.., thanks again for the sanity check 👍

Share this post


Link to post
Share on other sites

Thanks @Mustafa-Online - I was pretty certain it was good, now the worse bit, working out what else I have here that's causing this ;)

  • Like 1

Share this post


Link to post
Share on other sites

Check $user with var_dump if it has the expected user. My guess is that you overwrite $user somewhere. Where do you have this code?

  • Like 2

Share this post


Link to post
Share on other sites

Thanks @tpr, I wondered if I had too and so previously had done a `print_r($user)` and got a dump that looked convincingly like an array of user info, so I assumed I had not overwritten it. But I now see I get the same dumped output whether logged in or not... So that seems to suggest it's not null, is an array but looks like maybe it's an array of stuff that is not the $user object!

Thanks for the pointer, I will go back and look harder at that now and report back here, cheers!

Share this post


Link to post
Share on other sites

:-[ Oh nos... I was convinced I had ruled out any chance it could be session data, browser cache, etc, for example opening the same URL in a browser I don't use for dev. But I think it was somehow polluted session from using the CodeKit url `http://myMacName.local:5757/` rather than the LAMP url that I tell CodeKit is where my site is running in one tab, and the LAMP (non-CodeKit) url in another tab.

tl;dr: $user may not behave as expected when previewing on a CodeKit URL, or if you do and another tab is open at the non-CodeKit URL.

Takeaway, check this first and/or if I insist on keeping both Admin and end-user view open, at different URLs, use two different browsers.

Share this post


Link to post
Share on other sites
2 hours ago, alan said:

I wondered if I had too and so previously had done a `print_r($user)` and got a dump that looked convincingly like an array of user info

 

Sorry if it seems like I am promoting Tracy here, but if you were using her, you could just do this in the Console panel and you'd instantly see if $user is an object and what the name of that user is. 

59ea34e926131_ScreenShot2017-10-20at10_38_31AM.png.e392b8063aa383e3962d26fdfffa0253.png

  • Like 3
  • Thanks 1

Share this post


Link to post
Share on other sites

@adrian not at all! I love being reminded of Tracy.

Off to go look it up again. I am embarrassed to say I looked briefly when you released Tracy and I think I saw some dependancy of mine that meant a slight question and I never went further.

I am very impressed to see that you can do per your screenshot, clearly I am a total newb' in some respects, when it comes to debugging, I will turn on, er, `debug` and that's about as far as I get, then it's adding `echo` here and there to see stuff.

tl;dr I am eXCITEd to go look at your module!

Thanks! 👍

  • Like 1

Share this post


Link to post
Share on other sites
7 minutes ago, adrian said:

Sorry if it seems like I am promoting Tracy here

Developing a website or whatever without Tracy is for masochists! Sorry I can't resist! :lol:

  • Like 5

Share this post


Link to post
Share on other sites

o_O
s t u n n e d
to see what I have been missing by not using Tracy.

THANK you again @adrian for the post and for Tracy! ^_^

  • Like 2

Share this post


Link to post
Share on other sites
51 minutes ago, adrian said:

Sorry if it seems like I am promoting Tracy here, but if you were using her

Never considered Tracy as a she :). But yeah, its a girl's name isn't it.

I'm gonna call Tracy a `she` from now on.

  • Like 5
  • Haha 1

Share this post


Link to post
Share on other sites

Been meaning to do it for awhile, this reminded me. Gonna go and install her now.

  • Like 1

Share this post


Link to post
Share on other sites

One of those times that even though I'm pretty pleased with my progress, a quick scroll in there tells me I've only just got over a few small rocks at the bottom of the massive PHP mountain.

59ea4d861f15e_ScreenShot2017-10-20at20_19_51.thumb.png.71d180eba0f693917d168e9b9638c908.png

Do you leave this on when a site goes live or uninstall it?

Share this post


Link to post
Share on other sites

Actually @alan - I should note that in some instances using the Console panel for this purpose may not achieve what you are looking for. If your template had overridden $user somewhere you might be better off doing bd($user) or bd($user->name) just before that isLoggedin() check so you know you are getting the value that is being used right there in your code.

You can do this in your code editor, or you can even do it in the Template Editor panel and use the "Test" button - that way no changes are even made to your template files, so no need to remove debug statements when you're done. Have a read here: https://processwire.com/blog/posts/introducing-tracy-debugger/#template-editor-panel

  • Like 1

Share this post


Link to post
Share on other sites

 

7 minutes ago, SamC said:

One of those times that even though I'm pretty pleased with my progress, a quick scroll in there tells me I've only just got over a few small rocks at the bottom of the massive PHP mountain.

Firstly, sorry about the number of settings in Tracy - that isn't a sign of your lack of PHP knowledge, but rather a sign of my indecisiveness :)

 

7 minutes ago, SamC said:

Do you leave this on when a site goes live or uninstall it?

Really this is up to you. I leave it running - it's not accessible to regular users anyway. In production mode it can send you emails when it logs errors. But if you want you can uncheck the "Enable Tracy" option so there is no load at all from it, although it's not noticeable in production mode anyway.

  • Like 2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Sergio
      I have this "editor" role, that has the "user-admin-all" permission.
      I tried several times, doing different things sets of permissions, but I can't make a user with this role being able to make another user an "editor" too. PW disables the "editor" checkbox. I read the documentation 3 times that my eyes cannot see what I'm missing anymore.
      Any clues?
    • By ridgedale
      Reference: PW 3.0.62 and uikit3 based site using the Regular-Master profile.
      The issue is that blog posts for the particular site in question are automatically formatted as /blog/<blog-post-title>. It is evident the path format should have been configured as /blog/<year>/<month>/<blog-post-title>.
      Please could someone advise whether or not it is possible to change the path format for the blog posts of an existing active site that uses the regular profile?
      Any assitance would be greatly appreciated.
    • By CarloC
      I'm using the LoginRegister module and I'm getting an internal server error when I try to login with a wrong password more than one time.
      I've found that the error comes from the SessionLoginThrottle.module, because, for security reasons, is better to prevent too many failed logins.
      Ok, good. But, am I wrong if I think it's too bad to show an Internal server error to the user instead of a simple error in template saying the user to wait X seconds to retry to login?
      Is there a way to do that? It would be great if I could get the error string and style it in the page the way I like.
      And in the SessionLoginThrottle admin configuration, I think that could be a good idea to be able to change the maximum number of login attempts before the error is shown.
    • By zaib
      In PW I created a user 'tester' and gave the access to create edit and new. Issue is I'm unable to see 'edit' link in front of each page link and pages that i want to edit is already created.
      any help in this will be great help.
       
      Thank you.


    • By darrenc
      I'm trying to understand how I could have users log in as "members", provide them with a customized experience, but still serve them cached pages?
      For example let's say I have 3 roles
      Guest Member SuperUser And let's say I have a NavBar type component that has a menu. Plus it has "log in!" if guest,  or "logged in as Mike Smith"
      I would want guests to see a cached site with guest-only page access. Members should also see cached pages, but their menu may have access to members-only pages or fields or information. Furthermore they might have a "logged in as Mike Smith" element. SuperUsers can stay uncached. How does one go about creating that cache so that visitors aren't constantly re-creating the menu? Or that when "Sally Baker" logs in, she doesn't see a homepage that is cached for "Mike Smith" with all his elements?
      Thank you in advance for any replies.