如何使用PHP检查验证MongoDB ID

4
我获得了查询字符串中的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');
    }  

请帮忙... 谢谢
2个回答

10

请注意,该函数不检查空值。在这种情况下,ObjectID构造函数不会抛出异常,但会生成一个新的ObjectID。 - gouy_e

1

自 ext-mongo 版本 1.5 起,您可以检查它

if(isset($_GET['uid']) && MongoId::isValid ($_GET['uid'])) { 
 // Your code here 
}

php.net mongoid.isvalid

的意思是:


此函数已弃用:http://php.net/manual/zh/mongoid.isvalid.php - hpaknia
1
是的,因为现在我们有新的mongodb扩展。回到2015年7月它还没有被弃用。 - Alexander Serkin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接