-1

I have a PHP page already written specifically for users to be directed to (sort of like a referrer link, and carries querystring values) from external sites. This page is not intended to have any theming or Drupal content, and only is supposed to show a a single block of text in the middle-center of page with the words PROCESSING.... The intended workflow here is as follows:

  1. User clicks on a link from an affiliate-type site that links to something along the lines of www.example.com?reciprocate.php?a=x&b=y&c=z in my Drupal website.
  2. reciprocate.php then checks if the user is currently logged in, and if not, presents the user with the Drupal login form, and preserves the querystring values in a post variable, and posts back to itself.
  3. If the user is logged in, then the page will fetch some data from an external source, process it along with the original querystring values.
  4. Depending on the results of the processing, the user will then be redirected to an appropriate URL (for authenticated users only) within the site.

I can't seem to find any way to do this with either Drupal core or contrib modules, and I know I should be creating a custom module to do this, but I honestly haven't got around to learning how to do that, but believe me I will soon! Therefore hopefully you will understand the approach I took...I know it's not great, but I'm trying to work with what little I know.

I've already created my page at the root of my site, with all of my processing code, but I just can't seem to access any Drupal globals using the following as a test:

<?php
    include("includes\bootstrap.inc");
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    global $user;
    if ($user->uid) {
       // do custom processing here
    } else {
       print drupal_get_form('user_login_block');
    }
?>

Please see if there is any way you can assist me, as I am really desperate. This may seem like a simple isolated block of code, but my site really relies on it. I thank you most humbly in advance!

  • Have you tried using user_is_logged_in()? You can find more about it here: api.drupal.org/api/drupal/modules!user!user.module/function/… – Denchev Dec 1 '12 at 23:48
  • Hi Thenchew and thanks for your time. I can try give this a bash, but I cant even get the globals to work properly. I turned on display_errors, and when I reloaded my page again, it spat back a long error message, starting with Notice: Use of undefined constant DRUPAL_ROOT... – maGz Dec 1 '12 at 23:56
  • PARTIALLY SOLVED!: drupal.stackexchange.com/questions/35427/… showed me the way for the error I encountered above. Now I just need for the login to post back to the same page instead of redirecting to home page. Any thoughts? – maGz Dec 2 '12 at 0:37
  • oh, and I had to use drupal_render(drupal_get_form('user_login_block')); for D7 – maGz Dec 2 '12 at 0:41
1

Your code should be similar to the index.php file that comes with Drupal, at least for the part that bootstraps Drupal.

define('DRUPAL_ROOT', getcwd());

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

if (user_is_logged_in()) {
  // do custom processing here
}
else {
  drupal_render(drupal_get_form('user_login_block'));
}

Drupal code requires the DRUPAL_ROOT constant to be defined, as that constant is used to include PHP files.

    case DRUPAL_BOOTSTRAP_SESSION:
      require_once DRUPAL_ROOT . '/' . variable_get('session_inc', 'includes/session.inc');
      drupal_session_initialize();
      break;

If you look at the errors in the server log, it is probable you will find some errors about the constant not being defined, or about files not being found by require_once.

I am assuming you copied your PHP file in the same directory that contains index.php, and update.php that comes with Drupal.
Otherwise, the generic code to bootstrap Drupal from an external PHP file is the following one, suggested in drupal_bootstrap() documentation.

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

Replace 'path/to/drupal' with the absolute path where the Drupal files are copied.

Instead of trying to render a specific form, I would rather redirect the users to the page containing the login form. This method allows you to set the destination of the login form, which where the users are redirected after they submitted the login form.

define('DRUPAL_ROOT', getcwd());

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

if (user_is_logged_in()) {
  // do custom processing here
}
else {
  // The following line is probably not necessary, but if the user access the page
  // with reciprocate.php?destination=anotherpage, s/he will be redirected to anotherpage.
  unset($_GET['destination']);

  drupal_goto('user/login', array('query' => array('destination' => str_replace('%2F', '/', rawurlencode('reciprocate.php?a=x&b=y&c=z')))));
}

With Drupal 8, the code to fully bootstrap Drupal is slightly different.

define('DRUPAL_ROOT', getcwd());

require_once DRUPAL_ROOT . '/core/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
define('DRUPAL_ROOT', '/path/to/drupal');
require_once DRUPAL_ROOT . '/core/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

The difference is the name of the directory containing the core files.

  • kiamlaluno, thanks! Your first code block is what I have (see my comment below my original post). This all works fine now, but the only thing I have left to do is redirect the user back to reciprocate.php after they log in from the same page. Then when the page reloads, the user_is_logged_in() condition will be checked again and my // do custom processing here will run. I'm trying the Login Destination module, but I unfortunately lose my querystring variables using it. Do you have any advice? – maGz Dec 2 '12 at 8:06
  • 1
    See the updated answer. drupal_goto() allows you to set the destination page to which the users are redirected once the login form has been submitted. You should probably verify what happens when the user click on the link reserved to who forgot the password, and is redirected to your page. – kiamlaluno Dec 2 '12 at 8:30
  • kiamlaluno, wow! thanks you. almost there...the redirect works, but my querystring values get lost unfortunately. If there is no other way around this, my only option would then be use $_SESSION variables – maGz Dec 2 '12 at 9:58
  • Ugh! I must be blind: I didn't see that part of the question. Stay tuned. – kiamlaluno Dec 2 '12 at 10:46
  • :) cool! thank u for your patience! I'm just wondering... array('destination' => 'reciprocate.php') can carry the querystring values, but I will most likely have to encode it so that it won't look like an extra set of querystring name-value pairs – maGz Dec 2 '12 at 10:49
0

SOLVED!!...I think :)

I decided to give the "Login Destination" module one last shot.

  1. Created a rule for reciprocate as its own destination under Login Destination configuration.
  2. Created a Page node, gave it some arbitrary title, and gave it a URL alias of reciprocate. This was associated as Node-10 in Drupal.
  3. In my theme, I cloned node.tpl.php and renamed it to node--10.tpl.php.
  4. In this file I wiped everything and added my code from my original post above, starting from global $user;... (omitting the DRUPAL_ROOT definition bit).
  5. Browsed to my new page (unauthenticated) and included some test querystring values: http://localhost/kindred.test/reciprocate?a=x&b=y&c=z
  6. Was presented with a login block, and after logging in, I was redirected back to /reciprocate with my querystring values intact!!
  7. My // do custom processing here ran successfully, and as a test pulled in my querystring values using $_GET

I'm really chuffed that worked out! took my entire weekend, but worth it in the end. Thank you though to kiamlaluno for your help and patience.

  • Also, I know I lose my want of a blank page, but I'll make it work out. Really speaking, users won't see anything happen as the url will change and they'll be redirected accordingly :) – maGz Dec 2 '12 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.