Lemmings19
Lemmings19

Reputation: 1481

How to forcibly redirect a user on command and in real-time?

I need to create an admin feature that will allow an admin to destroy a user's session and redirect them to a new page in real time.

For example:

  1. A user named Bob is active on the website, viewing a page.
  2. An admin named Jill opens up a control panel and presses a button to forcibly log Bob out.
  3. On the server, Bob's session is destroyed.
  4. In Bob's browser and in real time, he is redirected to a page notifying him that he's been logged out.

I know how to remove Bob's session and force him to log back in when he goes to view a new page. But I do not know how to forcibly redirect Bob in real time. I'm guessing I need some kind of an active connection with Bob via Javascript so that I can push the command.

How would I do that? The only real-time Javascript knowledge I have is with AJAX, and that's usually triggered from the client-side and not the server-side.

I am using PHP 7.3 and CodeIgniter4 on the back-end. Currently no libraries on the front-end.

Upvotes: 1

Views: 56

Answers (1)

Devsi Odedra
Devsi Odedra

Reputation: 5322

You can use codeigniter 4 events. EVENTS

use CodeIgniter\Events\Events;

From admin side you can trigger some events

\CodeIgniter\Events\Events::trigger('some_events', $foo, $bar, $baz);

and on user side you can use on to get that event

Events::on('some_event', function($foo, $bar, $baz) {
        ...
});

Note : codeigniter 4 is not ready for production yet.

Upvotes: 1

Related Questions