Jump to content

Getting the currently logged in user.


brfcs
Go to solution Solved by Nathan Explosion,

Recommended Posts

Our site is split into a few sections, the main ones being the forum and the "editorial" style content on the front page. They both sit on the same domain so share cookies etc, and I want to be able to make use of the fact that a viewer is logged in so we can customise content for them, and have some interactive content where we use their ID or some token as a unique identifier.

If a user hits the forum first, they have a cookie set `ips4_member_id` but the TTL on this cookie is a couple of hours ago so it doesn't persist even as long as the session.

Is there a way to get the info I need? Just a unique identifier for the logged in user.

Link to comment
Share on other sites

<?php
require_once 'test/ips_47_test/init.php';
\IPS\Session\Front::i();
$member = \IPS\Member::loggedIn();
if($member->member_id){
    echo "Logged in";
} else {
    echo "not logged in";
}

You'll need to modify the 'require_once' path to match up to the location where the init.php for IPS is located based on the location of your script file (my php file is in root, the IPS install is in test/ips_47_test/)

Edited by Nathan Explosion
Link to comment
Share on other sites

  • 2 weeks later...

The reason I was asking about the cookie was some of my users were seeing really odd behaviour (and I saw it myself too).

If I clicked a link to a specific page from an email or Discord, that tried to access the value of the cookie it would fail, and when I inspected the page, the cookie wasn't present, none of the IPS ones were.

However, type the same link in and it worked fine. It wasn't a case of the cookie being set by the visit from Discord because I could click the link again and see the same thing.

I had a hunch it was down to the way the cookies were being loaded, I didn't know if the browser would load any stored cookies before making the initial call. It should, but it was the only scenario I could think of where they wouldn't be available.

I had a bit of a shuffle round with where in the lifecycle the cookie is accessed by my app (VueJS & NuxtJS based) and the issue seems to have gone away, but because I don't know the cause of the issue I'm not 100% sure it's properly fixed.

Link to comment
Share on other sites

  • Recently Browsing   0 members

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