Jump to content
dweeda

In PHP file from a redirect, $user->name is 'guest' after successful $user login in user in PW file.

Recommended Posts

In a PW file, I successfully login a $user using $session->login().

I later redirect to a different PHP file. It's not a PW template file so I do the appropriate  include("/path/to/processwire/index.php");

But then I try to get the logged in $user->name and it always gives me 'guest'.  And if I set a $session value at login, in the redirected file my values are lost.

I am new to PW so I am surprised.  What am I missing?

Thx

Share this post


Link to post
Share on other sites

hi dweeda and welcome to the forum,

not sure what you are trying to do exactly. maybe you can be more clear about what you are trying to achieve? if you need the script to run as a special user you can use https://processwire.com/api/ref/session/force-login/

but your question sounds a bit strange. maybe (as often in pw) there is a much more elegant solution and you don't need all this bootstrapping ;)

  • Like 1

Share this post


Link to post
Share on other sites

Bootstrapping processwire does skip the whole web request stuff, so theirs neither a current_user nor any $input items and other things dependent on a webrequest. It's only meant for api work and it cannot piggyback onto a webrequest of the php script pw was included into.

  • Like 3

Share this post


Link to post
Share on other sites

@dweeda, it would probably be better to put your code into a template file. Then instead of redirecting to the PHP file you redirect to some page URL that uses the template file. That way you can use the $user and $session variables as expected.

  • Like 2

Share this post


Link to post
Share on other sites

Thanks for all your replies.

To be more specific: before I started using PW I wrote a web app that's fairly robust (50+ php files) and is not needing content management.  I since wrote a website using PW that redirects to the app.  The app has its own login/reg handling, and was using PHP's $_SESSION to handle user timeouts, but that had a resilient $_SESSION bug where I needed to login twice the first time in to get the global var to be available.

A friend who uses PW suggested I use $user and $session to handle user logins and timeouts, so I brought the login file into PW. But I don't want to bring in all my 50+ app files as they have no need of content management.  I thought the PW index include would work, and I do see the $user and $session vars in my app, but they appear to be reinitiated and don't have my values I set in the login file inside PW.

Share this post


Link to post
Share on other sites
7 hours ago, bernhard said:

hi dweeda and welcome to the forum,

not sure what you are trying to do exactly. maybe you can be more clear about what you are trying to achieve? if you need the script to run as a special user you can use https://processwire.com/api/ref/session/force-login/

but your question sounds a bit strange. maybe (as often in pw) there is a much more elegant solution and you don't need all this bootstrapping ;)

To be more specific: before I started using PW I wrote a web app that's fairly robust (50+ php files) and is not needing content management.  I since wrote a website using PW that redirects to the app.  The app has its own login/reg handling, and was using PHP's $_SESSION to handle user timeouts, but that had a resilient $_SESSION bug where I needed to login twice the first time in to get the global var to be available.

A friend who uses PW suggested I use $user and $session to handle user logins and timeouts, so I brought the login file into PW. But I don't want to bring in all my 50+ app files as they have no need of content management.  I thought the PW index include would work, and I do see the $user and $session vars in my app, but they appear to be reinitiated and don't have my values I set in the login file inside PW.

Share this post


Link to post
Share on other sites

These variables do still exist, but in their default states. As I said, processwire does not handle the webrequest if it's bootstrapped into another script. So if you want processwire to do so you need to bring your other php files into processwire as well even if you don't need the content manangement tools, but you need the session/user tools. 

The other method, which I really don't recommend, is to try to copy the user initialization from /wire/core/ProcessWire.php. It might work, but each processwire update can potentially break it.

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Anton
      Hi there,
      I'm working with Processwire 3. Before summer I had issues to load to my backend. I finally managed to connect thanks to this : 
      $admin = wire('users')->get('admin');
      $admin->setOutputFormatting(false);
      $admin->set('pass', 'yo12345ZZ')
      $admin->save('pass');
      But later, the problem evolved: when I logged in to the site, the login page redirected me to the home. I didn't find any information about it on the web.
      But recently, the redirection has changed, now it is the login page that reloads when you connect. 
      I don't know where to start to fix this. It looks like sorcery.
      Thank you for all the help you can give me.
       
    • By angelo, italy
      Hi guys,
      I've always used WP but I want to swtich to PW. I'm not sure ....
      I'd like to know if it's possible to create a website for an online photo contest.
      The participants of the competition could create their own account, in which they upload their photos. The photos uploaded remain visible only to themselves and the judges.
      From their account they can make the "entrance fee" payment.
      The judges of the competition can create their own account... entering they see the photos of the participants and vote photos
      At the main page I imagine the title of the competition, a button to read the regulation, and a button to register.
      The website should be in Italian and English.
      Thank you!!
       
       
    • By SwimToWin
      ProcessWire is setting a "wires" cookie for each guest session.
      Is it possible drop that cookies, so there are no cookies at all for guests?
      That way, I don't need to spam the user with a cookie consent box.
      I don't need cookies for user preferences and marketing purposes.
      (Why are cookies being set by default in the first place?)
    • By celfred
      Hello,
      I'm struggling with this : a 'group' field of 'Page' type.
      The parent of selectable pages is '/groups', the template is 'group', and a member can create some groups in the /groups tree. I would like this user to see only the groups he or she has created so in my 'Find selector' in the backend, I would like to use :

      template=group, created_users_id=$user->id But that doesn't seem to work... I still get the list of all available groups in the /groups tree.
      Any idea ?
      I had a feeling I had already seen that before (something like $user->id must be replaced by users_id or something, but I can't find anything in the Forums... and all my tests keep failing...
    • By Peter Knight
      I have a demo site which I moved to a new VPS for client testing
      We noticed that leaving a page open and then revisiting the site can result in a 25 second(ish) to load time and will then throw a 500 Error.
      The hosting guys had a look and confirmed that the server is fine but the issue could be related to authentication or sessions.
      We are running Page Protector and ProCache so I wondered if there were any known bugs here and any recommended actions.
      My actual PW log doesn't show anything but the server log has plenty of these
       
      2018-12-06 08:14:00 Error xxx.141.1x.101 500 POST /who-we-are/ HTTP/1.0     1.58 K Apache access 2018-12-06 08:14:45 Warning xxx.141.1x.131   mod_fcgid: read data timeout in 45 seconds, referer: http://demo.abc.not/who-we-are/       Apache error 2018-12-06 08:14:45 Error xxx.141.1x.131   End of script output before headers: index.php, referer: http://demo.abc.not/who-we-are/       Apache error 2018-12-06 09:03:18 Error xxx.141.1x.131   2614#0: *667 recv() failed (104: Connection reset by peer) while reading response header from upstream       nginx error Thanks
      P
×
×
  • Create New...