Saturday 5 October 2013

simple login page using PHP and MongoDB.

simple login page using PHP and MongoDB.


Installation

Windows
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/

You can download PHP mongo drive/extension dll file from following URL https://github.com/mongodb/mongo-php-driver/downloads. Copy php_mongo.dll file into your PHP extension directory

Linux & Mac
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/

http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/

Connecting into MongoDB
PHP code connecting with out Authentication.
<?php
$mongo = new Mongo();
$db = $mongo->selectDB(“test”);
?>

With Authentication
<?php
$mongo = new Mongo(“mongodb://{$username}:{$password}@{$host}”);
$db = $mongo->selectDB(“test”);
?>

By default MongoDB contains sample database called “test”.

Creating New Database
$db = $mongo->Database_Name;

Note: Technically, you don't need to manually create databases in MongoDB due to its schemaless "lazy" way of creating databases.

Queries
PHP code to get MongoDB databases.
//Get list of databases
$mongo->admin->command(array(“listDatabases” => 1));
//Get list of Collections in test db
$db->listCollections();

Mongo Shell/Terminal/Command Prompt
db.listDatabases
db.test.showCollections

Create Collection(table)
You can created a collection/table but executing following code.
$db->createCollection(“people”,false);

Note: here false referred to unlimitted size, if you give true you have to mention the size of maximum.

Mongo Shell
db.createCollection(“people”,false);

Insert Record
Insert records int MongoDB.
<?php
$people = $db->people;
$insert = array(“user” => “demo@9lessons.info”, “password” => md5(“demo_password”));
$db->insert($insert);
?>

Mongo Shell:
db.people.insert({user:”user_name”,password:”password”});

Update Records
Update records into MongoDB
<?php
$update = array(“$set” => array(“user” => “demo@9lessons.info”));
$where = array(“password” => “password”);
$people->update($where,$update);
?>

Mongo Shell:
db.people.update({password:”password”},{$set : {user:”demo@demo.com”}});

HTML Code
<form action="index.php" method="POST">
Email:
<input type="text" id="usr_email" name="usr_email"  />
Password:
<input type="password" id="usr_password" name="usr_password" />  
<input  name="submitForm" id="submitForm" type="submit" value="Login" />
</form>

PHP Code
<?php
$succss = "";
if(isset($_POST) and $_POST['submitForm'] == "Login" )
{
$usr_email = mysql_escape_string($_POST['usr_email']);
$usr_password = mysql_escape_string($_POST['usr_password']);
$error = array();
// Email Validation
if(empty($usr_email) or !filter_var($usr_email,FILTER_SANITIZE_EMAIL))
{
$error[] = "Empty or invalid email address";
}
if(empty($usr_password)){
$error[] = "Enter your password";
}
if(count($error) == 0){
$con = new Mongo();
if($con){
// Select Database
$db = $con->test;
// Select Collection
$people = $db->people;
$qry = array("user" => $usr_email,"password" => md5($usr_password));
$result = $people->findOne($qry);
if($result){
$success = "You are successully loggedIn";
// Rest of code up to you....
}
} else {
die("Mongo DB not installed");
}
}
}
?>

2 comments:

  1. looks awesome but I didn't get that md5 on a login page! Normally the hash of a password should be done in an "add new user" page, and then in a login page u just have to compare that hash with the $_POST['password']..
    Does it make sens?

    ReplyDelete
  2. This post is useful for everyone. Thanks for sharing this type of post
    Full Stack Training in Hyderabad
    Full Stack Training in Ameerpet

    ReplyDelete