Im execution drupal from an external php file having following code

chdir($_SERVER["DOCUMENT_ROOT"]);
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
global $user;

But when I print $user object , Its always an anonymous user even if user is logged in to the site.
But This strange behavior is happening on one of the servers. Im using the same php code with same drupal version 6.22 on development server, Everything works fine there.

Thanks in advace

Comments

Elementica’s picture

I'm interested in this topic: I have a complex site configuration with a custom framework (name it "CF") side-by-side a D6 installation...

+----+   +----+
| CF |<->| D6 |
+----+   +----+

From D6 I need to access CF and I wrote a module for this (everything's fine).
From CF I need to access D6 and this is a problem: I think it's a matter of sessions, but I'm not sure what to check.
(I have a control in the code to prevent a "loop" while booting frameworks, as 1st calls 2nd, 2nd calls 1st and so on...)

I call drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); after setting $base_url and $cookie_domain and I use chdir to jump to D's folder.

In my CF I don't use global variables.

It seems a problem with session's id (php's session_id() function), because querying D's DB I see logged users and nobody has current session's id.

Elementica’s picture

Title: While executing drupal from external php script user object is always anonymous user » Login into D6 programmatically within another script

.

Under webroot I have:
/d6 (installation of D6)
/cf (installation of another custom framework)

I need to access from D6 to CF and back. I've written a custom module to get into CF from D6.

My main problem is to get into D6 from CF programmatically, mainly to perform a login.

I have a custom file in drupal's root folder, included in CF, with something like

	$drupal_path = dirname(__FILE__);
	$cdir = getcwd();
	chdir($drupal_path);
	$base_url = 'http://'.$_SERVER['HTTP_HOST'];
	$cookie_domain = $_SERVER['HTTP_HOST'];
	require_once './includes/bootstrap.inc';
	drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
	chdir($cdir);

(I found conflicts using sessions, so in CF I use sessions no more)

.

If I log into D6 (/d6/user) user_is_logged_in() returns false in CF.

If I log (calling the function with an existing username-password) using

	function drupal_user_login($username = false, $password = false) {
		$form_state = array();
		$form_state['values']['name'] = $username;
		$form_state['values']['name'] = $password;
		$form_state['values']['op'] = "Log in";
		return (drupal_execute('user_login', $form_state));
	};

and then navigate to D6 I'm not logged in, while user_is_logged_in() returns the correct uid in CF. i.e. I could log in the same browser, using the same account, in D6 with two separate sessions: within CF (programmatically) and within D6's interface (using log-in form): I think, so, it's a matter of session's id.

MY AIM IS to access D6 functions inside the other framework and I'd like that login/logout can be performed either programmatically or via D's interface.

.

Elementica’s picture

Presently I'm trying:

// Log in ($username and $password contain proper data):
$logindata = array(
	'name' => $username,
	'pass' => $password
);
drupal_user_logout(); // force logout of possible logged-in user
$uid = user_authenticate($logindata);
$user = user_load($uid);

It works, but if I have pages .../cf and .../cf/subpage sessions appear different, so if I log in the first one, I'm anonymous in the other and vice-versa. I think this is the same problem so that logging directly inside D6 doesn't work in CF and vice-versa.

I'm not sure what to check...

Elementica’s picture

For future reference.

Partially solved.

Setting cookie domain of D (variable $cookie_domain) in settings.php to ".example.com" (being not example, actually, but the real domain used) I can log into D6 and retrieve user logged in CF.

Meanwhile I'm trying to log from CF directly as user_authenticate(...) doesn't work (switching to D6 there's no user logged).
$user = user_authenticate(...) does the job in returning logged user back to the variable, but it seems that session is somehow "invalid", as switching to any other page of CF or even to D6 no user is logged in.

Elementica’s picture

Title: Login into D6 programmatically within another script » Login into D6 programmatically within another script [SOLVED]
Status: Active » Closed (works as designed)

For future reference.

Solved.

Digging into sessions, I've found problems with session_id, so investigating further I've seen session's id got erased somewhere... looking around I've found an unwanted session_destroy and erased it.

b33tl3’s picture

hello Elementica,
where did you find your unwanted "session_destroy" ?

I'm facing a similar problem.

- I got a php script that :
- log a user programmaticaly (via user_authenticate)
- and do a redirection toward a ubercart cart page (drupal_goto)

when I am on the cart page, each time I "update", "remove" items in the cart or checkout the cart, I got an error with the message "Validation error, please try again. If this error persists, please contact the site administrator".

It happens ONLY with safari. works OK with chrome and firefox !!!

thanks :)