I am building a website and want to integrate the login system. Can i simply add a copy of the drupal login form and check for session variables to integrate into my own system.

If this is true what is the name of the session variable that i can check for.

Comments

dman’s picture

The Drupal user management system is built on the rest of Drupal. There is no form you can just copy without the rest of the Drupal bootstrap.

It's conceivable you could run Drupal alongside your own 'website' and maybe even detect the logged in state. try print_r($_REQUEST) to see the cookie id. But you will need to include and run the entire Drupal API to get access to the actual username account for example.

Basically, no. Not with out so much development and fiddling that you might as well have integrated your website into Drupal rather than vice versa.

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

gpk’s picture

As dman says it might be easiest just to build your site using Drupal as your application framework (so that your custom stuff becomes Drupal module(s)).

However if your site already has a fair bit of functionality then you can "plug in" Drupal's session/user handling by adding:

require_once 'path/to/drupal/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

to your script(s). This actually gives full access to Drupal's API http://api.drupal.org/api/5 and you can then show a Drupal login/registration form, have Drupal process it etc., and access global $user to check the $user->uid (non-zero iff logged in) etc.

gpk
----
www.alexoria.co.uk

dman’s picture

Interesting. I was not aware that the API had become that pluggable. Could be possible then!

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

gpk’s picture

I think one of the authors of the Pro Drupal Development book mentioned something about using Drupal just for it's session/user facilities in one of their first serious encounters with/use of Drupal.

Possibly it's been that pluggable for a while. Or possibly I've completely misconstrued ;-) !!

gpk
----
www.alexoria.co.uk

mucello’s picture

I'm working on a standalone script but need access to the session variables. I tried the above approach but am not having success. My session ID ends up being different from a session ID in use on pages made in drupal. Any ideas why?

gpk’s picture

Does the script use the same domain as the Drupal site?

gpk
----
www.alexoria.co.uk

mucello’s picture

Yes, same domain and even the same machine. I just need to access $_SESSION variables already set by Drupal. But when I require() the bootstrap.inc code and do drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION), my session ID changes and any session variables previously set are not accessible. For some reason, my implementation starts a new session rather than using the existing session.

gpk’s picture

It sounds like the Drupal site and your other script are using different cookies for some reason -> different sessions. Suggest examining your cookies for the domain in question, and possibly deleting same (and/or all your sessions in the {sessions} table.

Where is your script located relative to Drupal's index.php?

gpk
----
www.alexoria.co.uk

mucello’s picture

Exactly, but I don't know why they are using different cookies. I tried your suggestion of emptying the sessions table, but that seemed to make no difference.

Drupal's index script is located in /home/www/html/
My script is located in /home/www/html/somewhere/else/query.php

The first few lines of query.php look like this:
...
chdir('/home/www/html/');
require('./includes/bootstrap.inc');
drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
echo 'sessionid='.session_id(); // just to see what my session ID is to manually compare to my browser's cookie session ID.
...

When the echo statement executes, I have a new cookie with a different session ID. When I go back and browse my drupal pages, I'm using the previous cookie and session.

What would cause this behavior? How do I get around it?

gpk’s picture

When you look at your cookies from within your browser, what is the difference between them?

I have a feeling that the latest versions of Drupal hash the domain and path of the script to get the cookie name (SESS........). You may need to put query.php in the same folder as index.php.

[Update: Yes, the session name is an MD5 hash of $base_url. See http://api.drupal.org/api/function/conf_init/5. Probably your script gets assigned a base url of www.example.com/somewhere/else. As an alternative to putting your script in the top level Drupal folder, you may be able to fix the problem by hard-coding $base_url in settings.php.]

gpk
----
www.alexoria.co.uk

mucello’s picture

That worked! I uncommented the $base_url line in settings.php and changed it to my domain. Now, my non-drupal scripts can effectively use Drupal sessions. Very cool. Thanks for your help!

gpk’s picture

This behaviour has changed slightly within Drupal 5.x as a result of a security fix (http://drupal.org/node/162361). Your problem wouldn't have arisen in 5.1, which had me confused for a while!

gpk
----
www.alexoria.co.uk

edill3484’s picture

I uncommented $base_url in settings.php and I was successfully able to access session variables from external php scripts on the same domain. Just be sure to change directories to the root before including the bootstrap file. Thanks for your help!