Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
function custom_user_login($edit, $account) {

  require_once $_SERVER['DOCUMENT_ROOT'] . "/mage1/app/Mage.php";
  global $user;

  $session = Mage::getSingleton('customer/session', array('name' => 'frontend'));
  try {
    $session->login('alvaro@test.com', 'cec123');

  } catch (Exception $e) {

i am using this set of code from my D7 installation to log-in to magento.But it is not loggin into either D7 or magento.

But when i run this code from a seperate simple php file outside it logs in to magento successfully. what could be the problem.

share|improve this question
can anybody suggest how to view the session and cookie info after a user is logged in – zamil Dec 24 '12 at 12:56
You've closed the try block with curly brace }, also the catch block; but you didn't close the function. – AjitS Dec 24 '12 at 13:13
@indrock: it was mistake when i copied it here. in code it is properly closed – zamil Dec 27 '12 at 7:13

The function signature is not right, which may be the reason for the code not executing. See the docs for hook_user_login function.

The parameter $edit is passed by reference and not directly by value. So, the correct definition your function should be,

function custom_user_login(&$edit, $account) {

  // rest of the code goes here (not duplicating).


Also make sure, your modules name is custom and it has files custom.info and custom.module in the module directory. And offcourse, enable the module ;-)

Also, to suggest you to look into the session and cookie info (based on your comment), you can put an echo '<pre>';print_r($_COOKIE); echo </pre> inside your theme's template.php file. Or simply install the Devel module, and enter dpm($_COOKIE); inside your template.php, to see a nicely formatted result. You'll need to clear the cache to again load the fresh template files. Similarly, you can print the $_SESSION superglobal to see the session information.

share|improve this answer
I made the $edit pass by reference, but still the output is same. not loggin into both D7 or magento. but am sure its loggin into D7 since it is entering the hook_user_login.I believe it is loggin out at some point. any insights from anybody – zamil Dec 27 '12 at 7:11
i tried to print the $_SESSION before and after login as u mentioned. but it returns an empty array.Cookie array i have added to your answer. plz see it. – zamil Dec 27 '12 at 9:04
up vote 0 down vote accepted

I used Rules module to set a rule and redirect after that event occurs.

Its not fully error proof. But it works. It logs into both Drupal and Magento

I will update this if i find some errors once i dig deep.

For the time being its fine

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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