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

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 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. 
       
       
    • By prestoav
      I have a site built with a user role 'member' that can view some extra 'member area' pages. I have a user with the role 'editor' that has been given 'user-admin' permission who can successfully generate new users. However, when they try to add / change a 'member' role password to a 'member' role they get the error:
      Unable to generate password hash Does anyone know how to assign the 'editor' role the permission to generate a password for other user types?
      Thanks!
    • By ridgedale
      Reference: PW 3.0.62 and uikit3 based site using the Regular-Master profile.
      I've setup a hidden page and template (sitePreferences - with no associated template file) containing site-wide preferences/settings, however I can't get the field content to display.
      I've tried the following which either display nothing or break the site:
      <?php echo $item->siteChairperson; ?> <-- nothing gets displayed <?php echo $field->siteChairperson; ?> <-- nothing gets displayed <?php =page()->siteChairperson ?> <-- breaks site <?php echo $field = $fields->get('siteChairperson'); ?> <-- displays the text siteChairperson instead of the name. <?php =sitePreferences()->siteChairperson?> <-- breaks site <?php echo $field->get($siteChairperson); ?> <-- breaks site Any assistance would be greatly appreciated.
    • By ridgedale
      Reference: PW 3.0.62 and uikit3 based site using the Regular-Master profile.
      I wonder if anyone has come across the problem of displaying images within the <figure> element before.
      When editing a page:
      If an image is added inside <p></p> tags without any align assigned, it displays normally at the size the user has specified. See screen grab example 1.
      If an image is placed inside the <figure> element and left or right alignment applied, the size the image is displayed as in the editing view is significantly reduced. See screen grab example 2.
      If an image is placed inside the <figure> element and center alignment applied, the size the image completely disappears in the editing view with only the caption text showing. See screen grab example 3.
      The code used to display an image in <p></p> tags without aligment assigned is standard:
      <p><img alt="" src="/<path-to-image>/image.jpg" width="128" /></p> Examples of the code automatically being generated by PW3/UiKit3 from within the image editing interface are:
      <figure class="align_left"><img alt="" src="/<path-to-image>/image.jpg>" width="128" /> <figcaption>Caption text</figcaption> </figure> <figure class="align_right"><img alt="" src="/<path-to-image>/image.jpg>" width="128" /> <figcaption>Caption text</figcaption> </figure> <figure class="align_center"><img alt="" src="/<path-to-image>/image.jpg>" width="128" /> <figcaption>Caption text</figcaption> </figure> Important Note: The images and and captions actually do display correctly when the website is browsed, but from the editing perspective it is not practical.
      Any thoughts or advice would be appreciated.