Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 9337

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!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>&quot;username&quot;</em> and <em>&quot;password&quot;</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 &quot;admin&quot; database</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Specifying&nbsp;the&nbsp;username&nbsp;and&nbsp;password&nbsp;in&nbsp;the&nbsp;connection&nbsp;URI&nbsp;(preferred)<br /></span><span style="color: #0000BB">$m&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</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">//&nbsp;Specifying&nbsp;the&nbsp;username&nbsp;and&nbsp;password&nbsp;via&nbsp;the&nbsp;options&nbsp;array&nbsp;(alternative)<br /></span><span style="color: #0000BB">$m&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">"mongodb://localhost"</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #DD0000">"username"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$username</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"password"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$password</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</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>&quot;db&quot;</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">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Specifying&nbsp;the&nbsp;authentication&nbsp;database&nbsp;in&nbsp;the&nbsp;connection&nbsp;URI&nbsp;(preferred)<br /></span><span style="color: #0000BB">$m&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</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">//&nbsp;Specifying&nbsp;the&nbsp;authentication&nbsp;database&nbsp;via&nbsp;the&nbsp;options&nbsp;array&nbsp;(alternative)<br /></span><span style="color: #0000BB">$m&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</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">,&nbsp;array(</span><span style="color: #DD0000">"db"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"myDatabase"</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</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>