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 anjalipv@yahoo.com on
Hello,
Objective :In my site I want to create a user programatically and make that user login when i submit a content type form .
Current status : I have created a user programatically using user_save, which actually does create user in database and I also added roles to it. Now how do i make that user login , I do have the user name and password values . how to make that user login ?
Urgent help required.
thanks in advance
Comments
I wrote a URL login module
I wrote a URL login module for a customer who didn't pay.
I guess I'll open source it shortly if payment isn't received. (It's been many months already so I'm 99% sure I'll open source it around xmas sometime)
Ray Smith
http://RaymondSmith.com
Ray Smith
http://RaymondSmith.com
not sure if this function
not sure if this function may help u ...
......user_external_login()
u may find this in user module.
Thanks for the reply.But
Thanks for the reply.
But could not find sucha a function "user_external_login()".(Drupal 5.x)
Here are some functions to help
I created these functions to do exactly what you're looking form. There was talk once about CRUD functions library. But nothing seemed to be able to do this at the time. so here is a breakdown
hope this helps
It is not working
Hi,
I am calling your function through webservice which returns all the variables resulting through user_authenticate function but the user was not found logged in when we open the url in other tab
But when we check log reports we found that user session has been created when we called that function.
So please if any one can help me telling the reason why is it so?
Whereas when we directly give link to a page on which we call drupal_execute('user_login', $form_state); function it works.
after doing this when we chnage tab we found the user logged in ?????
but is not working when want it to do through web service.
Cookies
When you say that you are running through a webservice, do you mean in the browser through javascript/AJAX/etc?
If so, I don't think you the server can set the cookies through AJAX. So you won't stay logged in through subsequent calls.
If you are using PHP/server side stuff.. to consume the webservice, you need to implement the cookies manually...
Off the bat for javascript/ajax, I see a few ways to get around this.
1. Have the user authenticate previously through the regular way, I.E. not through a webservice, create a simple log in page, which will redirect him to the page that you want to user to use the webservice on. This set the proper cookies in the browser, and will then send it over to the server in the AJAX calls. Just make sure that the domain names are the same, for the log in page, and the webservice endpoint.
In such a case you won't need to call user_authentication. Downside, is that you can only be logged in as one user at one time.
2. Implement one of the fancy webservice authentication modules such as OAuth, http://drupal.org/project/oauth which is the way to do authentication with webservices, this is how Google and the big boys accomplish authentication. In such a case calling user_authentication won't work... :-(, this process will work for PHP consumption too.
Unlike the previous suggestion, you can be logged in as multiple users if you implement this correctly.
PS> the purpose of the above function, or the reason why I created them initially, I wanted to perform some modification on the system, like, add/remove/update node, If I did it directly with the database or with the superuser I would not have any record of who created the nodes etc. The functions allow me to create a script that will login as a specific user (say autobot) and then perform the changes and drupal will track the user and I can then troubleshoot it later if necessary, or assign specific permissions to that user to limit it's functionality.
All the best.
How to set cookies
Hi,
Thanks yfreeman for your quick reponse
I am actually calling a web service from a .NET Site to php drupal site
The function which the web service calling is defined in a page on root of drupal
You are correct,the cookie not getting set,but how to do that-:
My code is -:
global $user;
global $account;
if ($user->uid) {
//user is already logged in
return services_error(t('Already logged in as !user.', array('!user' => $user->name)));
}
$user = user_authenticate(array('name' => $username, 'pass' => $password));
if ($user->uid) {
session_start();
$return = new stdClass();
$return->sessid = session_id();
$return->user = $user;
setcookie(session_name(), session_id(), 0, '/'); // I added this to make sure the cookie is set
return $return;
}
session_destroy();
return services_error(t('Wrong username or password.'));
But it is not working,session is generating but the cookies content is different,not the session id
How to solve it,I want to do it through web service only,just my job is to return successfull after loging in
Then users in . NET site will just click on the menu in their site and will directly land in drupal site
I can do it by directly giving the link to any page in drupal site and again redirect to .NET site but this is not the correct way to do that
Plz show me the correct way
Thanks
Cookies cont.
If I understand correctly you have a .NET backend communcating with a drupal site, as a webservice (the .NET part)
When drupal sends the cookies over to .NET, they do not get passed over to the browser. Rather .NET will pass it's own set of cookies to the browser.
This is the problem that webservices had when the stated out. So they came out with OAuth. http://en.wikipedia.org/wiki/OAuth . google has something else called ClientLogin Authentication, but it addressed the same principle.
This allows for a separate set of credentials to run side by side, across multiple sites.
Setting the cookie in drupal won't help.
Yeah I know it's complicated. Just the way it works.
The only way I see around it is if you cache the drupal cookies in the .NET code/db (don't send it to the browse) and in the subsequent drupal calls in .NET you set the cookies in the HTTP requests to the ones previously received.
I'm not sure how to do this in .NET, i'm not as proficient
Good luck
Again
I re-read you reply, and I'm still confused about a couple of point.
All you want to do is log the user in to the drupal site., through .NET backend? that's it?
Yes,Only LOGIN
Hi yfreeman,
At present,my only concern is to make the user logged in-:
I was doing this by giving a link from .NET site to page on Drupal site where my code is written.
This is functioning properly,no problem
BUT I DONT WANT THIS
i was told to call this code in a function through web service and the result is as discussed previously "cookie is not set" .
oauth module is something to get the user data to other site and the user is not necessary to be present there.(I think so)
That can be achieved by any way,if security is not a concern.
WSOD on user login
Thanks for the functions. They look correct but every time I call user_authenticate or user_load, I get a Whitescreen of Death (WSOD). I've tried installing the WSOD module and going through the WSOD Drupal pages:
http://drupal.org/node/158043
and
http://drupal.org/node/482956
but haven't found a solution.
Do you know what would cause a WSOD when logging in a user?
you need to do something after
just running the function will probably produce a WSOD. you need to do something afterward. I.e. redirect to a page
drupal_redirect() etc.
You could use user_load()
You could use user_load() yourself to avoid querying the database directly:
$user = user_load(array('mail' => $mail, 'pass' => trim($pass), 'status' => 1));
OR
Just override the global Variable $user
Not sure whether it's permitted !
Simplest way
The simplest way that I've found is just by setting the global $user variable with a user object... this is of course if you don't need a password.
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
How to login programmatically into Drupal
Drupal 8 Drupal Create User PHP User Object user_save save_user