The following warnings occurred:
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.2.25 (Linux)
File Line Function
/global.php(961) : eval()'d code 26 errorHandler->error
/global.php 961 eval
/showthread.php 28 require_once





× This forum is read only. As of July 23, 2019, the UserSpice forums have been closed. To receive support, please join our Discord by clicking here. Thank you!

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Private Pages (Twig)
#1
I'm sorry if this has been asked before, but I've been poking around the web for the last two days and can't seem to find quite what I'm looking for. (Also, I'm new here, so I think I'm allotted some leeway?)

I'm use Slim 3/Twig, but I don't think either of those is the problem (could be wrong). I'm able to load up every page just fine, having set UserSpice aside to load without Twig rendering (basically, got my templates in one directory and UserSpice user pages in another). I can get all of the appropriate pages to show up in the Admin panel (having edited the z_us_root.php array), and I can add or remove permissions/private viewing just fine to any of the pages.

The problem is that I can't seem to figure out exactly how to require the init.php file or the securePage function in my controller so that it will properly check for login status/status and redirect as appropriate.

For example, when a user tries to access example.com/privatepage without being logged in.

I do apologize if this is a silly question. I feel like the answer is so simple and I'm overthinking it.

Thank you in advance.
  Reply
#2
Hi whatwilldo,

How're you? Welcome!

Call the init using a require once and your securePage. Try this:
<?php require_once '../users/init.php';?>
<?php if (!securePage($_SERVER['PHP_SELF'])){die();} ?>

The ../ gets you out of your current directory and to the init file...there are other ways you can do this but that is the best way I have find currently...let me know what else you need.

Also note: I know nothing about Slim and Twig so what I'm saying very well could be wrong!

B.
  Reply
#3
Thanks for the quick reply, Brandin!

If I put that directly on the page (privatepage.php might be the page in my example), and I type in the path to access it directly, all of the UserSpice code works as it's intended.

But I have something like this set-up on my index page:

Code:
require_once 'users/init.php';

<pre>
Code:
$app->get('/privatepage', function ($request, $response, $args) {
        
    return $this->view->render($response, 'privatepage.php');
    
});
</pre>



Because it will render privatepage.php using Twig, I can't put the PHP code you provided in directly (or at least I don't know HOW to with the Twig syntax, which might also be the problem). So, I was trying to execute it prior to the return, but I can't seem to figure out how to get that to work (or if it's possible) so that:

If user logged in, return ...->render, etc.
but If not logged in, redirect to log in.
  Reply
#4
Code for logged in after calling init:
if($user->isLoggedIn()) { //whatever }

if(!$user->isLoggedIn()) { //whatever not logged in }

Let me know if that helps!

B.
  Reply
#5
Brandin, you're amazing. Thank you! The answer was that simple.

For anyone else that might be running into the same problem as I, I just threw all that code after the get->... function (my mistake was having it all split up in the first place). That also meant moving my user->... statements.

Thanks for being so responsive. Sometimes we just need to hear our thoughts out loud to see what exactly is going on.
  Reply
#6
Let us know if you need anything else Smile Glad to hear it is working.
  Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)