2
votes

I've written some educational web apps (HTML, js) for my kids. I've gotten to the point where I need to start tracking their progress individually, so the apps can focus more on what the individual still needs to learn, and spend less time repeating what they already know well.

To do that, I've created a Drupal site, with logins for each of the kids. Once they log in thru Drupal, I would like to have my app call some php pages (which I will write) that access Drupal's authentication states.

So my question is, if I'm writing php pages that query or update a database, how can these pages first ascertain whether the user is logged in to Drupal on that site, and if so, get the name of the currently authenticated user?

Many thanks for any pointers.

5

5 Answers

3
votes

Start your pages with this code (taken from Drupal's index.php)

define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

You may need to adjust the paths if your scripts aren't in the same folder as Drupal's index.php.

Then you have full access to the Drupal API in your script, and the user's session will be loaded. You'll need to bootstrap Drupal with this code before the suggestion above will work. From there, you can do

global $user;
print_r($user);

Now you can do things like:

$other_user = user_load($some_user_id);

In the long run you really would get a lot of benefits from building this as a Drupal module.

2
votes

I thinks its easier to create a Drupal module from your PHP scripts. You won't have to worry about authentication then. Just my opinion.

2
votes

You can use the services module

A standardized solution of integrating external applications with Drupal. Service callbacks may be used with multiple interfaces like REST, XMLRPC, JSON, JSON-RPC, SOAP, AMF, etc. This allows a Drupal site to provide web services via multiple interfaces while using the same callback code.

Update:

If that's the case, you can write the code within drupal. No need for external applications.

To find out the current logged in user, use this code sample:

global $user;
print_r($user); // print out the user object

Hope this helps... Muhammad.

1
votes

The answer to this problem is here : Drupal - using boostrap to check logged in user outside of Drupal not working

The bootstrap code works, you just need to set the $cookie_domain in settings.php

0
votes

this file should be inside drupal installation It is hell easy

//set the working directory to your Drupal root
define("DRUPAL_ROOT",     "/var/www/drupal/");

//require the bootstrap include
require_once 'includes/bootstrap.inc';

//Load Drupal
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); 
//(loads everything, but doesn't render anything)

$name = 'admin';
$password = 'admin';

//use drupal user_authenticate function
if(!user_authenticate($name, $password)){
    echo 'invalid';
}else{
    echo 'valid';
}