i get mongodb id of query string. how to check validation this id? i want if mongodb id not valid redirect to another url
get query string:
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');
}
plz help... thanks
In new PHP Mongo library https://github.com/mongodb/mongo-php-library the best way to check validity of a string is: (thanks to Yii2 code 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;
}
}
Since ext-mongo version 1.5 you can check it
if(isset($_GET['uid']) && MongoId::isValid ($_GET['uid'])) {
// Your code here
}