Jump to content

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


dweeda
 Share

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

Link to comment
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
Link to comment
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
Link to comment
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
Link to comment
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.

Link to comment
Share on other sites

  On 7/11/2017 at 7:38 AM, 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 ;)

Expand  

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.

Link to comment
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
Link to comment
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
 Share

×
×
  • Create New...