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 ridgedale
      Reference: PW 3.0.111 and uikit3 based site using the Regular-Master profile.
      Despite my searches of the forum I'm somewhat confused about how to create new child pages on the frontend when a user clicks on a button on the parent page. I also have an equivalent button that is intended for uploading a .csv file to automatically create multiple new pages. This basically relates to a club (parent) and members (child) template configuration. Hopefully this explanation makes sense.
      A button should be able to launch the code needed to initiate the script required to create a new page using something like:
      <a href="/path/page.php">New +</a> <a href="/path/page.php">New ++</a> Does the code to create the new page or new pages need to be run from the template file for the child or the parent?
      A new individual member page will need to be editable manually at the point of page creation as well as subsequently, whereas multiple new pages will need to be editable after they have created and populated with data, again, as well as subsequently.
      I would very grateful for any advice or pointers as to how to achieve this.
       
    • By ridgedale
      Reference: PW 3.0.111 and uikit3 based site using the Regular-Master profile.
      I was wondering if there is a way to restrict user navigation to specific pages.
      Login (home.php - not to be displayed)
          |__  About (not to be displayed)
          |__  Clubs (not to be displayed)
          |            |__ Club (to be displayed)
          |                       |__  Club Members (to be displayed)
          |__ League (not to be displayed)
          |            |__Season (not to be displayed)
          |                       |__  Match (not to be displayed)
          |__  News (blog.php -  to be displayed)
          |
      etc, etc
      Based on the above the navigation needs to appear simply as:
      ---------------------------------------------------------------------------
                           Club    Club Members    News    
      ---------------------------------------------------------------------------
      Any thoughts appreciated.
    • By ridgedale
      Reference: PW 3.0.111 and uikit3 based site using the Regular-Master profile.
      I am trying to create a process whereby when a user logs in to their profile page (the user is automatically redirected to their profile page on login) and they then go to their 'members' page and creates a child page ('club-member'), the data stored in the user template ($user->usercode) is automatically added to the equivalent field on the club-member page.  This will be applicable only to the template used for the 'club-member' pages. Once the new page has been created the equivalent 'usercode' field on the 'club-member' page should not be subsequently editable.
      Below is the page hierarchy for visual reference:
      Login (home)
          |__  profile
                      |__  members
                                     |__  club-members
      It seems like an AddHookBefore might be the solution here such as:
      wire()->addHookBefore("Pages::saveReady", function($event) { $page = $event->arguments(0); if($page->template == 'club-member') { $clubcode = $user->club_code; $field = $page->club_code; $field->set('value', $clubcode); } }); I am I on the right track or is there a better way to achieve this?
      Any thoughts appreciated.
    • By ridgedale
      Reference: PW 3.0.111 and uikit3 based site using the Regular-Master profile.
      I've setup a page where a member can edit contact details via the frontend displaying the field content using the <edit> ... </edit> tags.
      This works fine when the fields actually contain data. However fields that contain no data (i.e. empty) do not appear to be editable. No edit cursor appears (- possibly owing to the field width being 0px?)
      Is the only solution to recreate the page using a form, for example,  or is there a simple way to allow blank fields to be editable on the frontend?
      I wondered if anyone else has found a solution to this problem. Any assistance would be appreciated.
    • By ridgedale
      Reference: PW 3.0.111 and uikit3 based site using the Regular-Master profile.
      I am trying to add a field that provides a dropdown menu but there are no Options or Selector(s) type available - see attached image of field types available.
      The following reference under the docs does not appear to be applicable any more:
      https://processwire.com/api/modules/select-options-fieldtype/
      I can't see how to achieve this. Any assistance would be appreciated.