文字

MongoDB::authenticate

(PECL mongo >=1.0.1)

MongoDB::authenticate登录到数据库

说明

public array MongoDB::authenticate ( string $username , string $password )

此方法能够认证它的连接。 如果数据库服务器开启了认证(默认没有开启),在做任何操作之前你需要登录。

通常情况下,你应该使用 MongoClient::__construct() 内置的参数而不是此方法。 如果你对一个连接验证了,然后在会话期间连接掉了然后重连,你会被重新验证。 如果你用这个方法手动验证,然后连接掉了,你必须在重新连接时手动调用这个方法。

该方法等同于运行:

<?php

$salted 
" ${ username } :mongo: ${ password } " ;
$hash  md5 ( $salted );

$nonce  $db -> command (array( "getnonce"  =>  1 ));

$saltedHash  md5 ( $nonce [ "nonce" ]. " ${ username }${ hash } " );

$result  $db -> command (array( "authenticate"  =>  1 ,
    
"user"  =>  $username ,
    
"nonce"  =>  $nonce [ "nonce" ],
    
"key"  =>  $saltedHash
));

?>

当一个连接验证后,它仅能够通过 "logout" 数据库命令来登出:

<?php

$db
-> command (array( "logout"  =>  1 ));

?>

参数

username

用户名。

password

密码(明文格式)。

返回值

返回数据库的响应,如果登录成功,它会返回

<?php
array( "ok"  =>  1 );
?>
如果出现了什么错误,它会返回
<?php
array( "ok"  =>  0 "errmsg"  =>  "auth fails" );
?>
("auth fails" 可能是另外的信息,取决于数据库版本和发生了什么错误)。

参见

MongoDB 关于 » authenticate 的核心文档。

更新日志

版本 说明
1.2.11 使用时产生 E_DEPRECATED 。 请将验证细节传入到构造器。
上一篇: 下一篇: