如何用php检查验证mongodb id

2 php validation mongodb mongoid mongodb-query

我得到查询字符串的mongodb id.如何检查验证此ID?我想如果mongodb id无效重定向到另一个url

获取查询字符串:

    if(count($_GET)>0 && $_GET['uid']){

            //get id from string query
            $query = array("_id" => new MongoId($_GET

['uid']));
        $user = DB::findone('users',$query);

    }else{
        //redirect if not exist query string
        header('location:'.ADMIN_URL.'/items/forbidden.php');
    }  
Run Code Online (Sandbox Code Playgroud)

请帮忙...谢谢

hpa*_*nia 6

在新的PHP Mongo库https://github.com/mongodb/mongo-php-library中检查字符串有效性的最佳方法是:(感谢Yii2代码https://github.com/yiisoft/yii2-mongodb/ blob/master/validators/MongoIdValidator.php)

   function isValid($value)
    {
        if ($value instanceof \MongoDB\BSON\ObjectID) {
            return true;
        }
        try {
            new \MongoDB\BSON\ObjectID($value);
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }
Run Code Online (Sandbox Code Playgroud)