check if logged in and if user is part of administrative group to see page

check if logged in and if user is part of administrative group to see page

by Jerry Lau -
Number of replies: 2

hello everyone.

moodle 3.6.8 on redhat 7.8 64 bit and mysql 7.7 and php 7.2.x

I am trying to learning moodle and one of the simple things I wish to learn is to how check if a user has already logged in to page I have built (a simple hello world string) and only those that have moodle administrative role are able to view that page.

For example my page, called test.php is located at https://ourmoodle.url.com/whatever/ folder

if a user tries to go to https://ourmoodle.url.com/whatever/test.php, it should first check to see if they have logged in and if not, they are redirected to the login page.

If they are logged in already, it checks to see if they are part of administrative group.

How do I accomplish that in my test.php page?

thanks

Jerry






Average of ratings: -
In reply to Jerry Lau

Re: check if logged in and if user is part of administrative group to see page

by Jerry Lau -
I have just figured it out... thank you.
In reply to Jerry Lau

Re: check if logged in and if user is part of administrative group to see page

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

In case anyone else finds this forum post and is looking for an answer to that question, the required code is:

$PAGE->set_url("/path/to/page.php"); // Important so Moodle knows where to return after the login page, if required.
require_login();
if (!is_siteadmin()) {
    die('Admin only'); // Possibly should be a proper localised error message, but only site admins should end up on this anyway ...
}