-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhow-to-programmatically-create-and-log-in-drupal-users.html
More file actions
166 lines (159 loc) · 6.53 KB
/
how-to-programmatically-create-and-log-in-drupal-users.html
File metadata and controls
166 lines (159 loc) · 6.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>Dreams of thought</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/theme/styles.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="/theme/slicknav.css">
<script src="/theme/jquery.slicknav.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<script type="text/javascript"
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.6.2/html5shiv.js"></script>
<![endif]-->
<script>
$(function(){
$('#menu').slicknav({'label':''});
});
</script>
<link rel="icon" href="/theme/favicon.ico" sizes="16x16 32x32 48x48 64x64" type="image/vnd.microsoft.icon">
<link rel="apple-touch-icon" sizes="114x114" href="/theme/apple-touch-icon-precomposed.png">
<link rel="apple-touch-icon" sizes="152x152" href="/theme/favicon152.png">
<link rel="apple-touch-icon" sizes="196x196" href="/theme/favicon196.png">
</head>
<body>
<div id="wrapper">
<header>
<nav class="byline"><ul id="menu">
</ul></nav><!-- /#menu -->
<div class="downloads">
<a href="#" class="fa fa-Twitter"></a>
<a href="#" class="fa fa-Github"></a>
</div>
<hgroup>
<h1>How to programmatically create and log in drupal users</h1>
<footer class="article-footer">
<address class="vcard author">
By <a class="url fn" href="./author/admin.html">admin</a>
</address>
<abbr class="published" title="2010-02-22T20:39:00+05:30">
on 22.02.2010
</abbr>
</footer><!-- /.post-info -->
</hgroup>
<meta name="tags" contents="drupal" />
<meta name="tags" contents="php" />
<meta name="tags" contents="user" />
</header>
<section id="content" class="body article">
<div class="entry-content">
<div class="line-block">
<div class="line">Creating a new user is very easy in Drupal 6. Here's how.</div>
<div class="line">[sourcecode language="php"]</div>
</div>
<p>></p>
<p>$new_user = array(</p>
<div class="line-block">
<div class="line">'name' => $username,</div>
<div class="line-block">
<div class="line">'mail' => $mail,</div>
<div class="line">'pass' => user_password(),</div>
<div class="line">'status' => 1,</div>
<div class="line">'auth_MODULENAME' => $username</div>
</div>
<div class="line">)</div>
</div>
<p>$user = user_save(NULL,$new_user)</p>
<p>// log the user in</p>
<p>$user = user_authenticate($new_user)</p>
<p>[/sourcecode]</p>
<p>Now for the explanation. We create a $new_user array with values we
want the newly created user to have. We pass this along to the
<a class="reference external" href="http://api.drupal.org/api/function/user_save/6">user_save</a> function
and set the 1st parameter as NULL. From the code comments in the user
module -</p>
<blockquote>
<div class="line-block">
<div class="line">* @param $account</div>
<div class="line-block">
<div class="line">* The $user object for the user to modify or add. If $user->uid is</div>
<div class="line">* omitted, a new user will be added.</div>
<div class="line">*</div>
<div class="line">* @param $array</div>
<div class="line">* (optional) An array of fields and values to save. For example,</div>
<div class="line">* array('name' => 'My name'); Setting a field to NULL deletes it from</div>
<div class="line">* the data column.</div>
</div>
</div>
</blockquote>
<p>So setting the 1st parameter as NULL creates a new user.</p>
<p>The parameters name, mail, pass and status are all self explanatory. The
<a class="reference external" href="http://api.drupal.org/api/function/user_password/6">user_password</a>
function generates a random password (by default with a length of 10).</p>
<p>The (optional) 'auth_MODULENAME' element will record the user as being
created externally. This will result in an entry in the authmap table
like this -</p>
<div class="line-block">
<div class="line">"aid" "uid" "authname" "module"</div>
<div class="line">"2" "20" "USERNAME" "MODULENAME"</div>
</div>
<p>The
<a class="reference external" href="http://api.drupal.org/api/function/user_authenticate/6">user_authenticate</a>
function logs the user in. This function expects an array as a
parameter. It first loads the user in using the
<a class="reference external" href="http://api.drupal.org/api/function/user_load/6">user_load</a> function
and in case of no errors logs the user in.</p>
<p>This approach of logging in a user is useful when we have the array with
us which contains the raw values used to create the user. If all you
have is the uid of the user, then logging the user in is very simple.
Just use the global $user object.</p>
<p>[sourcecode language="php"]</p>
<p>global $user</p>
<p>$account = user_load( array('name' => 'USERNAME') ); // or
user_load(UID)</p>
<p>$user = $account</p>
<p>[/sourcecode]</p>
<p>Don't use the user_authenticate function here as it expects the raw
values(form values). This wil not work -</p>
<p>[sourcecode language="php"]</p>
<p>$account = user_load( array('name' => 'USERNAME') ); // or
user_load(UID)</p>
<p>user_authenticate((array)$account)</p>
<p>[/sourcecode]</p>
<p>It will take whatever password is stored in the user object(the raw
password), md5 it and then run a query to load that user. Since the
password in the query is not the raw password but the md5, the query
will return nothing. This will cause an error in the user_authenticate
function.</p>
<p>For a direct code example for programmatic log in, check the
<a class="reference external" href="http://drupal.org/project/Devel">devel</a> module's devel_switch_user
function.</p>
</div><!-- /.entry-content -->
<div class="article-share-tags">
<div class="end-article-tags">
<i class="fa fa-tags"></i>
<a href="./tag/drupal.html">drupal</a>
<a href="./tag/php.html">php</a>
<a href="./tag/user.html">user</a>
</div>
<div class='article-share'>
share -
</div>
</div>
</section>
<div class="neighbors">
</div>
<footer>
<a href="./pages/about.html" >About</a>
<div class="right-footer">
<a href="./categories.html" >Categories</a>
<a href="./tags.html" >Tags</a>
</div>
</footer>
</body>
</html>