<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Authentication</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongo.connecting.html">Connecting</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongo.connecting.rs.html">Replica Sets</a></div> <div class="up"><a href="mongo.connecting.html">Connecting</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="mongo.connecting.auth" class="section"> <h2 class="title">Authentication</h2> <p class="para"> If MongoDB is started with the <em>--auth</em> or <em>--keyFile</em> options, you must authenticate before you can do any operations with the driver. You may authenticate a connection by specifying the username and password in either the connection URI or the <em>"username"</em> and <em>"password"</em> options for <span class="function"><a href="mongoclient.construct.html" class="function">MongoClient::__construct()</a></span>. </p> <div class="example" id="mongo.connecting.auth-example"> <p><strong>Example #1 Authenticating against the "admin" database</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Specifying the username and password in the connection URI (preferred)<br /></span><span style="color: #0000BB">$m </span><span style="color: #007700">= new </span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">"mongodb://</span><span style="color: #007700">${</span><span style="color: #0000BB">username</span><span style="color: #007700">}</span><span style="color: #DD0000">:</span><span style="color: #007700">${</span><span style="color: #0000BB">password</span><span style="color: #007700">}</span><span style="color: #DD0000">@localhost"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Specifying the username and password via the options array (alternative)<br /></span><span style="color: #0000BB">$m </span><span style="color: #007700">= new </span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">"mongodb://localhost"</span><span style="color: #007700">, array(</span><span style="color: #DD0000">"username" </span><span style="color: #007700">=> </span><span style="color: #0000BB">$username</span><span style="color: #007700">, </span><span style="color: #DD0000">"password" </span><span style="color: #007700">=> </span><span style="color: #0000BB">$password</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <p class="para"> By default, the driver will authenticate against the <em>admin</em> database. You may authenticate against a different database by specifying it in either the connection URI or the <em>"db"</em> option for <span class="function"><a href="mongoclient.construct.html" class="function">MongoClient::__construct()</a></span>. </p> <div class="example" id="mongo.connecting.auth-db-example"> <p><strong>Example #2 Authenticating against normal databases</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Specifying the authentication database in the connection URI (preferred)<br /></span><span style="color: #0000BB">$m </span><span style="color: #007700">= new </span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">"mongodb://</span><span style="color: #007700">${</span><span style="color: #0000BB">username</span><span style="color: #007700">}</span><span style="color: #DD0000">:</span><span style="color: #007700">${</span><span style="color: #0000BB">password</span><span style="color: #007700">}</span><span style="color: #DD0000">@localhost/myDatabase"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Specifying the authentication database via the options array (alternative)<br /></span><span style="color: #0000BB">$m </span><span style="color: #007700">= new </span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">"mongodb://</span><span style="color: #007700">${</span><span style="color: #0000BB">username</span><span style="color: #007700">}</span><span style="color: #DD0000">:</span><span style="color: #007700">${</span><span style="color: #0000BB">password</span><span style="color: #007700">}</span><span style="color: #DD0000">@localhost"</span><span style="color: #007700">, array(</span><span style="color: #DD0000">"db" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"myDatabase"</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <p class="para"> If your connection is dropped, the driver will automatically attempt to reconnect and reauthenticate you. </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongo.connecting.html">Connecting</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongo.connecting.rs.html">Replica Sets</a></div> <div class="up"><a href="mongo.connecting.html">Connecting</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>