I'm try to perform a form that are provide an email and a name field when my index open. When the user sets that fields the page check if the email exists so if dont, it create a user using user_save and then log the in. I perform all of this but user login.

I use that to perform user login:


global $user;
	$uid=user_authenticate('user','user_pass');
	$user = user_load($uid);

After that the user login but don't have session..

Someone could help me??

thank you very much indeed

Comments

devtherock’s picture

I think session should be started after login, you can try this : drupal_session_start()
http://api.drupal.org/api/drupal/includes--session.inc/function/drupal_s...

thanks

Kuldev

bboldi’s picture

Take a look at this http://api.drupal.org/api/drupal/modules--user--user.module/function/use...

I think that this is the right way (have not tested it, just wrote from scratch so you may need to debug it):


        global $user;

        $logindata = array(
            'name' => 'username',
            'pass' => 'password'
        );

        $uid = user_authenticate($logindata);

        $logindata['uid'] = $uid;

        user_login_finalize($logindata);

W.M.’s picture

But is it safe to login programatically to a Drupal 7 site using a custom PHP file which bootstraps Drupal and gets login credentials via a POST request? Thanks.

Isn't it safer to login using the Drupal login form?

prsnjtbarman’s picture

$uid = user_authenticate($username, $password);

$user = user_load($uid);

$form_state = array();
$form_state['uid'] = $uid;
user_login_submit(array(), $form_state);

vinoth.babu’s picture

//login
$uid = user_authenticate($username, $password);
global $user;
$user = user_load($uid);
//login finalize
watchdog('remote_user', 'Session opened for %name.', array('%name' => $user->name));
$user->login = REQUEST_TIME;
db_update('users')
->fields(array('login' => $user->login))
->condition('uid', $user->uid)
->execute();
drupal_session_regenerate();

vindesh’s picture

Sometimes we need to login into our Drupal site not through the regular login form. For example if we have trouble with login form or maybe we just forgot our password... Here are few ways to login into your Drupal site not through login form.
For Drupal 8 you just need to create file your_auto_login.php in the Drupal root directory, put there the next code and open this file in your browser:
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
$autoloader = require_once 'autoload.php';
$kernel = new DrupalKernel('prod', $autoloader);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);

/**************start******************/
// ID of the user.
$uid = THE_AUTOMATIC_USER_ID; //Do not use 1 because it is the super admin

IF you forgate password of drupal 8

You need to put index.php at root
$uid ='Your User ID Here';
$account = \Drupal\user\Entity\User::load($uid);
\Drupal::service('session')->migrate();
\Drupal::service('session')->set('uid', $account->id());
\Drupal::moduleHandler()->invokeAll('user_login', array($account));

or
$uid ='Your User ID Here';
$user = Drupal\user\Entity\User::load($uid);
user_login_finalize($user);

/*********end of code*************/

$response->send();
$kernel->terminate($request, $response);

Recover password using Drush:
If Forget password
If you forget the password, use drush to recover
drush uli

Login programmatically as a user | Drupal 8

JAINV18’s picture

Hi, I am trying to use this code to login use on successful authentication by OAuth2, but facing error for contextual render. Can you help me why am I facing this issue

Martin Joergensen’s picture

This looks pretty simple, but for some reason I can't get it to work.

I need to login users based on a URL with a token in it - bypassing the login form, and I thought it would be exactly this easy. But the session cookie for the user isn't created and the user is logged in OK, but immediately out again on the next page load. What gives?

Here's my simple code:

    $user = User::load(9); // @todo find uid based on $token from URL
    user_login_finalize($user);
    $response = new RedirectResponse('/');
    $response->send();
    die();

The user is registered as logged in in the log (Session started for...)

If I check the user is authenticated with \Drupal::currentUser()->isAuthenticated() after the user_login_finalize() function has been called, it's OK.

But when reaching the front page after the redirect, it's back to anonymous.

Martin

arsh244’s picture


Hi Martin, if you are using any version greater than 9.2.5, this could possible fix the issue:

$user = User::load(9); // @todo find uid based on $token from URL  
$response = new RedirectResponse( '/' );
$request  = \Drupal::request();
$request->getSession()->save();
$response->prepare($request);
\Drupal::service('kernel')->terminate($request, $response);
$response->send();
die();

Let me know if that works!!!

Martin Joergensen’s picture

@arsh244,

It worked!

You just forgot the line that actually does the login, namely the function user_login_finalize()

When I added that, it went smoothly, and now logs in the user and stores the session:

    $user = User::load(9); // @todo find uid based on $token from URL  
    user_login_finalize($user);
    $response = new RedirectResponse('/');
    $request  = \Drupal::request();
    $request->getSession()->save();
    $response->prepare($request);
    \Drupal::service('kernel')->terminate($request, $response);
    $response->send();
    die();

Thanks a bunch!

Martin

someshver’s picture

Here is the Full Code

  $user = \Drupal\user\Entity\User::load(1); // @todo find uid based on $token from URL
  user_login_finalize($user);
  $response = new \Symfony\Component\HttpFoundation\RedirectResponse('/');
  $request  = \Drupal::request();
  $request->getSession()->save();
  $response->prepare($request);
  \Drupal::service('kernel')->terminate($request, $response);
  $response->send();
  die();