Moodle 4.0 + Check if user is logged into Moodle

Moodle 4.0 + Check if user is logged into Moodle

by Deepa Vaitheeswaran -
Number of replies: 2

I am a newbie to Moodle.  I have installed Moodle 4.0 on Ubuntu and am using the Moove theme

I'd like to check if the user is logged into Moodle in an external PHP script which is in a subdirectory of my Moodle installation.

My code is below

require_once('../config.php');

if (!isloggedin()) {

     echo "not logged in<br>";

} else {

echo "logged in<br>";

}

This redirects me to my site home page as soon as the require_once('../config.php'); is reached.  

Please can someone tell me how I can change this behaviour?

Average of ratings: -
In reply to Deepa Vaitheeswaran

Re: Moodle 4.0 + Check if user is logged into Moodle

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

That require_once('../config.php'); does not normally redirect, but it will in some exceptional circumstances. For example:

  • If Moodle has not been installed on your server, it will redirect to start the installation process.
  • If you have updated the code on the server to a new major version, then it will redirect to start the upgrade process.

It sounds like you are seeing something different to this, but I am sure there are other exceptions.

Probalby, to work out what is going on you will have to trace through the code in your debugger, to see what is happening. If you are going to be doing a lot of Moodle development, then acutally this is not a waste of time. Getting some idea what all the Moodle standard setup code does is useful knowledge to have.

In reply to Tim Hunt

Re: Moodle 4.0 + Check if user is logged into Moodle

by Deepa Vaitheeswaran -

Thank you for your response.

I followed your advice and checked the debugger which showed no errors.  So I went back and did a fresh install and changed only the theme and it works.

It works on my old installation too after I removed all the plugins that I had added to test the reporting features.