Know if a user is authenticated on panel in the public website

Hello,

I’d like to know how can I know in my public website if a user is logged in in panel.

Thanks

<?php if ($user = $site->user()) {
  echo "The user is logged in";
}
?>

If you need to check for a specific user or role, you can do that as well.

http://k2.getkirby.com/docs/cheatsheet#user

That’s what I was trying but I always get NULL, nevertheless if I use the same code in panel controllers everything works fine.

What could be happening?

Thanks

Where are you using this code? In a template, a plugin?

In a controller on the public site

Hm, that’s strange; could you please post the complete code?

What are you trying to achieve, anyway?

In home.php:

<?php
return function($site, $pages, $page) {

    if ($user = $site->user()) {
        echo "The user is logged in";
    }
    else {
        echo "No user";
    }

And you always get “no user”, no matter if you are logged in or not? That’s weird.

Exactly! I’ve spent hours trying to understand this. Any ideas?

So lets say you log into the Panel, open the home page in the Panel and then click on the preview link, you still see “No user” and if you go back into the Panel you are still logged in?

Yes, but I see that the preview link is simple link, nothing special on it, only a target="_blank", that’s correct?

Yes, the preview link is just a link.

And we are talking about Kirby 2 here, not Kirby 3?

Yes:

static public $version = '2.5.10';

Hm, I know that the code works. Could you update to 2.5.12 (the latest version)?

I don’t know because this is not my project and I don’t know if I’ll break something :confused:

I’ll try to do it making a full backup.

There’s any update guide?

Updating means removing the kirby and panel folders and replacing them with the new ones.

Thanks, I’ll try it and reply here the result

Exact same result :sob:

Hm, are you testing locally or is the project online somewhere?

I’m trying locally but the website is published and working