Jump to content

refer a PHP session that is created out side of PW  inside PW


SIERRA
 Share

Recommended Posts

I have configured the procsswire in a sub folder like  https://www.example.com/pw/. Here www.example.com is a non processwire website. In 'pw' folder I have configured processwire. In www.example.com users will be logging in and I am trying to referrer that session variable inside processwire website.

If I create test page in 'pw' folder like  https://www.example.com/pw/test.php and if I refer the logged in session variable this is working

test.php - is as below

session_start();
echo "User ID".$_SESSION["user_id"];

But  if I include the above code in head.inc.php this is not working

Please suggest how can I refer a PHP session that is created out side of PW  inside PW

My requirement is as below

Some pages in ttps://www.example.com/pw/ should be available only to the logged in users.  Users will be login using  https://www.example.com/. So I am trying to refer that session inside PW

Thanks

Link to comment
Share on other sites

I don't think I've ever had to do this before, but now that I gave it a quick try, for me it seemed to work right out of the box:

// test.php
session_start();
if (!isset($_SESSION['user_id'])) {
    $_SESSION['user_id'] = date('j.n.Y H:i:s');
}
echo "User ID for " . session_id() . " " . session_name() . " is " . $_SESSION["user_id"];
// processwire template file
echo "User ID for " . session_id() . " " . session_name() . " is " . $_SESSION["user_id"];

... and the user_id values were the same. Note that you can't start multiple sessions at once, so if you also included the session_start() part in your template file, this is redundant (and might cause an error). At this point ProcessWire has already started the session.

If you do this, is the returned value from $_SESSION["user_id"] empty / unset?

57 minutes ago, SIERRA said:

Do I have the possibility to convert $_SESSION["user_id"] to $sesssion->userid;

Technically yes, though I'm not sure how/why that would be useful. Just assign the value: $session->userid = $_SESSION["user_id"].

Note that if you're trying to log the user in based on the user_id value from another system, that's a different question entirely.

Link to comment
Share on other sites

Hi teppo,

I am able to get the SESSION variable's value in header

Here I am trying to provide access to the logged in users to some of the PW's pages. But the user will be logging in from another system. In PW I am just checking the session alone.

Thanks

 

Link to comment
Share on other sites

22 hours ago, SIERRA said:

Here I am trying to provide access to the logged in users to some of the PW's pages. But the user will be logging in from another system. In PW I am just checking the session alone.

Right. Well, you can read the user_id variable from $_SESSION and you can be confident that nothing else will insert it (and it being there means that the user has a valid login session in the other system), would it be enough to check this and perform a redirect to somewhere (home page perhaps) if it's not found from $_SESSION?

That'd be the easy way out, anyway ?

If you definitely want to log the user in to ProcessWire, you could do that as well, but for that to work you would also need a local (ProcessWire) user. This could be a single user account that's shared by all the users coming from another system, or you could individual accounts for each user. To log the user in with a specific user would be as simple as $session->forceLogin('name-of-the-shared-user'). Individual user accounts would require a few more steps; you'd have to create the user (if it doesn't exist yet), log the user in with it, and so on.

If you go with the "real ProcessWire login" approach, keep in mind that these sessions don't automatically have anything to do with the sessions of that other system, so you should also make sure that the external session is still valid, and if not, then log the user out of Processwire.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...