Example of PHP MongoDB simple user Login implementation

Source: Internet
Author: User
Tags html form md5 mongodb php mongodb


With the popularization of NoSQL database management system, the data storage of many software has turned to MongoDB database. It uses dynamic mode to transform data into structured JSON document storage to improve application performance.

In this chapter we learn to use PHP and MongoDB to implement simple user login capabilities.


Before you learn this tutorial, please make sure there is already a PHP MONGO driver, if you haven't, please download it via the following address:


Windows:


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


Linux and Mac


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



Connect to MongoDB


No authentication connection

<?php
$mongo = new MONGO ();
$db = $mongo->selectdb ("test");
? >


Verifying connections

<?php
$mongo = new MONGO ("mongodb://{$username}:{$password}@{$host}");
$db = $mongo->selectdb ("test");
? >


By default, MongoDB has a sample database of "test". Or you can create a new database:


$ db = $ MONGO-> database_name;



Inquire


PHP Get Database list

Gets the list of databases
$mongo->admin->command (Array ("listdatabases" => 1));
Gets the table of the test database
$db->listcollections ();


MONGO use the following command at the terminal can also achieve the above query effect:


Db.listdatabases
db.test.showCollections


Create a collection (table)


PHP Statement Creation table


$db->createcollection ("People", false);
False here indicates an infinite size, and if true, the maximum space for the table must be specified.


MONGO Terminal command CREATE TABLE:


$db->createcollection ("People", false);


Insert Record


PHP Code Insert Record

<?php
$people = $db->people;
$insert = Array ("User" => "demo@uncletoo.com", "Password" => MD5 ("Demo_password"));
$db->insert ($insert);
>

MONGO terminal command Insert record


B.people.insert ({User: "user_name", Password: "Password"});


Update records


PHP Code Implementation Update MongoDB

<?php
$update = Array ("$set" => Array ("User" => "Demo@9lessons.info"));
$where = Array ("Password" => "password");
$people->update ($where, $update);
>


Mongo Terminal Command Implementation update


Db.people.update ({password: "password"},{$set: {User: "Demo@uncletoo.com"}});


HTML form


<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>



Complete PHP Code index.php


<?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 ...
}
} else {
die ("Mongo DB not Installed");}}
? >

This article describes the PHP MongoDB implementation of simple user login example, I hope this article can bring inspiration to readers, help readers solve the problem, thank you for reading this article.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.