Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By hllvd on
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
I think session should be
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
Take a look at this
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):
But is it safe to login
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?
Drupal User Login Automatically
$uid = user_authenticate($username, $password);
$user = user_load($uid);
$form_state = array();
$form_state['uid'] = $uid;
user_login_submit(array(), $form_state);
Login pro-grammatically. This code really works
//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();
How to login programmatically into Drupal 8
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
Trying this approach but it fails
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
This doesn't work for me in D9
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:
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
Try this once
Hi Martin, if you are using any version greater than 9.2.5, this could possible fix the issue:
Let me know if that works!!!
Worked!
@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:
Thanks a bunch!
Martin
Here is the Full Code
Here is the Full Code