Building a simple login page using MongoDB and PHP

registration form in php and mongodb
php mongodb example
php vs mongodb
how to retrieve data from mongodb using php
php mongodb crud tutorial
php mongodb create database
mongodb query in php
mongodb collection php

I am trying to connect my login page to Mongodb but i am encountering some error.Since I am trying it for the first time I am not completely familiar to this.I would like to know where I have gone wrong This is the html page

<html>
<head>
<title> Login</title>
</head>
<body>
<form action="login.php" method="POST">
User Name:
 <input type="text" id="username" name="username" />
 Password:
  <input type="password" id="password" name="password" />
<input name="submit" id="submit" type="submit" value="Login" />
</form>
</body>
</html>

This is PHP code:

    <html>
 <?php
 if(isset($_POST['submit'])
  {
   $username = ($_POST['username']);
  $password = ($_POST['password']);
    if(empty($username))
    {
     echo "Empty or invalid email address";
    }
     if(empty($password)){
     echo "Enter your password"; 
      }
      $con = new MongoClient();
     // Select Database
     if($con)
      {
      $db = $con->tickets;
      // Select Collection
    $collection = $db->Admin;
     $qry = array("username" => $username,"password" => md5($password));
      $result = $collection->findOne($qry);
    if($result){
     echo "You are successully loggedIn";
       }
    else
     { echo "unsuccessful";
     }

      } else { 
      die("Mongo DB not connected");
      } 
        }
      ?>
       </html>

There is nothing specific that would make it end just before ticket. So I don't think there is an exact answer that can be given.

However in cases like this, try narrow it down to a smaller problem, for example, does this work:

<html>
<body>
<?php
echo "Hello";
?>
</body>
</html>

If no, then you probably don't have PHP installed correctly or possibly not configured correctly.

If yes, build it up a tad:

<html>
<body>
<?php
echo $_POST['submitForm'];
?>
</body>
</html>

Etc, etc

As a side note, I am not encouraging the mix of PHP/HTML nor the direct use of $_POST - I am just building on the original question

PHP MongoDB - Part 9 - Login and Register Form, Note: Technically, you don't need to manually create databases in MongoDB due to its schemaless "lazy" way of creating databases. Queries Building web applications. The web applications being built for this particular MongoDB guide is an RSS aggregator like Google Reader. The web applications will have two key components: A REST API and a Feed grabber. To understand what a REST API is, you need to first know a little about the MEAN stack.

You may want to use empty function on the $result.

So your php code must be like this:

<?php
if(isset($_POST['username']) && isset($_POST['password'])){
    $username = ($_POST['username']);
    $password = ($_POST['password']);
    $pass_hash = md5($password);
    if(empty($username)){
        die("Empty or invalid email address");
    }
    if(empty($password)){
        die("Enter your password");
    }
    $con = new MongoClient();
    // Select Database
    if($con){
        $db = $con->tickets;
        // Select Collection
        $collection = $db->admin;   // you may use 'admin' instead of 'Admin'
        $qry = array("username" => $username, "password" => $pass_hash);
        $result = $collection->findOne($qry);

        if(!empty($result)){
            echo "You are successfully loggedIn";
        }else{
            echo "Wrong combination of username and password";
        }
    }else{
        die("Mongo DB not connected!");
    }
}
?>

Also check your collection name. Is that 'admin' or 'Admin'

Hope this will help.

simple login page using PHP and MongoDB., Build a simple CRUD app with MongoDB in PHP - Thien Tran Duy explains how. Head on over to the downloads page on the MongoDB official website. This is where you have defined the database name, username and  The MongoDB server is built to already work with your current web server, but not PHP. To make PHP talk to the MongoDB server, we are going to need the PHP-MongoDB driver. It is a PHP extension

I Used same code but the unsuccessful login error occurs for me and I fixed it.hope it will be helpful!

try this

$result = $collection->findOne(array('username' =>'$username,'password' =>md5($password)));

Instead of

$qry = array("username" => $username,"password" => md5($password));
$result = $collection->findOne($qry);

Building a Simple Blog App with MongoDB and PHP, The focus of this guide is just that: How to make use of MongoDB for pretty typical between a pretty basic programming exercise, and a NoSQL solution might work. <br /> <form class=”form-horizontal” action=”index.php”  In the demo-node-app folder create an app.js file and a start.js file. Also create a routes folder, with an index.js file inside. After you’re done, things should look like this: . ├── app

Getting Started with PHP and MongoDB - Joseph D. Marhee, There are no user contributed notes for this page. MongoDB · authenticate · command · _​_​construct · createCollection · createDBRef · drop · dropCollection  This mobile application tutorial shows you how to create a user registration, login and logout backend using MongoDB and Mongoose. This article is part of a series of mobile application

MongoDB::authenticate - Manual, Doctrine Doctrine MongoDB Bundle Documentation: How to implement a simple In this example, we'll create a registration form with some extra fields and (like a terms accepted src/Acme/AccountBundle/Document/User.php namespace  Implementing User Authentication Mechanism. User authentication is very common in modern web application. It is a security mechanism that is used to restrict unauthorized access to member-only areas and tools on a site. In this tutorial we'll create a simple registration and login system using the PHP and MySQL.

https://github.com/vladakilov/Simple-User-Authenti, Next Page. To use MongoDB with PHP, you need to use MongoDB PHP driver. Download the driver from the url Download PHP Driver. Make sure to download  Login and registration in NodeJS, Express with MongoDB. How to perform login and registration activity in nodeJs. As everyone knows JavaScript is beating all the programming languages day by day. This is because they are developing new frameworks which really cool as performance-based and they are enough to solve all the problems at a very low …

Comments
  • what 'some error' are you getting?
  • its showing unsuccessful even though the data is present in the database. I am able to retrieve the username and password in other php program. I think it is not quering properly. I would like to know where i have gone wrong.
  • after the query check what error you get with var_dump($mongo->lastError());
  • I have not used $mongo variable anywhere in the code. it shows undefined variable
  • Php is working fine. Moreover it is connecting to Database. I am able to insert using php. Just this login is not working
  • The code is working fine but its giving unsuccessful login even though the username and password is stored in DB. Is there any other method to compare username and password? @Paras
  • are you sure your collection name is 'Admin'? because usually collection names do not contain capital letters. I tries the code on my PC and it seems to work file. @Shraddha
  • I changed the collection name as admin but still showing unsuccessful login
  • you are using not empty function write? If it does not work may be there is some typo error.