skip to Main Content

I am trying to make a website where users have to log in, through the password and username fields. If the username and password are correct, the website has to show a message saying that the log has been successful. It is important that the password field is vulnerable to nosql injections, like "[$ne]=1". The goal is that if I put a user that exists and the nosql injection, I should get the message "the log was successful".

The problem is that I have to connect to a mongoDB database, but it tells me that I am not connected correctly. I get the following error:

( ! ) Fatal error: Uncaught Error: Class "MongoDBClient" not found in C:wamp64wwwprac1mongoxd.php on line 10

( ! ) Error: Class "MongoDBClient" not found in C:wamp64wwwprac1mongoxd.php on line 10

What is wrong with my code? In case you are wondering, I have installed the mongodb extension in PHP.

<?php


if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
    $username = $_POST["username"];
    $password = $_POST["password"];

    
    $mongoClient = new MongoDBClient("mongodb://localhost:27017");

 
    $collection = $mongoClient->Users->users_info;
    $cursor = $collection->find(['Name' => $_GET['Name'], 'Password' => $_GET['Password']]);

   
    if ($cursor->count() > 0) {
        echo "Log successful";
    } else {
        echo "Error";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login (Inseguro)</title>
</head>
<body>
    <h2>Login</h2>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
        <label for="username">User name:</label>
        <input type="text" name="username" required><br>

        <label for="password">Password:</label>
        <input type="text" name="password" required><br>

        <input type="submit" value="Login">
    </form>
</body>
</html>

2

Answers


  1. How did you installed MongoDBClient?

    Be aware that if you installed via Composer like so:

    composer require mongodb/mongodb
    

    You should include an autoload like this:

    require_once 'vendor/autoload.php';
    

    Other than that, there’s no need for an extra backslash before the word "new", you can change it like:

    $mongoClient = new MongoDBClient("mongodb://localhost:27017");
    

    Also, be sure that the extension is enabled on your php.ini file

    Hope that helped!

    Login or Signup to reply.
    1. If you installed the MongoDBClient using composer, you should add

      require ‘vendor/autoload.php’;

    in the top most of the file

    1. If you downloaded a zip file or installed by downloading, you may use

      require ‘directory_of_MongoDB/the_class_file_name.php’;

    Your error means all though you might have MongoDB installed but it has not been included in the file being executed. In this case, consider point 2 above if you didn’t install it via composer. Thanks

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search