mongodb php远程连接

时间:2015-11-07 22:03:09

标签: php mongodb steam

我有一个与Mongodb一起使用的脚本有代码

  <?php
if(isset($_SESSION["steamid"])) {
include_once('steamauth/userInfo.php');
    $connection = new MongoClient();
    $collection = $connection->selectDB('admin')->selectCollection('site_users');
    $list = $collection->find();
    $steam_list = array();
    foreach($list as $key => $value){
        $steam_list[] = $value['steamid'];
    }

    if(!in_array($_SESSION['steamid'],$steam_list)){
        $collection->save(array('steamid' => $_SESSION['steamid'],'name' => $_SESSION['steam_personaname'],'ip' => $_SERVER['REMOTE_ADDR']));
    }

?>

这意味着当用户使用他的Steam帐户登录时连接到Mongodb 第一个问题在这里代码尝试连接到本地Mongodb服务器如果是的话我想将此连接转换为远程连接所以ican使用MongolabDb托管它,等待你帮助家伙谢谢:)

1 个答案:

答案 0 :(得分:0)

MongoCollection :: find 方法返回MongoCursor

需要转换结果。 iterator_to_array

<?php
if(isset($_SESSION["steamid"])) {
    include_once('steamauth/userInfo.php');
    $connection = new MongoClient();
    $collection = $connection->selectDB('admin')->selectCollection('site_users');
    $cursor = $collection->find();
    $list=iterator_to_array($cursor)
    $steam_list = array();
    foreach($list as $key => $value){
        $steam_list[] = $value['steamid'];
    }

    if(!in_array($_SESSION['steamid'],$steam_list)){
        $collection->save(array('steamid' => $_SESSION['steamid'],'name' => $_SESSION['steam_personaname'],'ip' => $_SERVER['REMOTE_ADDR']));
    }

?>