MongoDB and PHP simple login
10,295
To get the username and password from user input you need to do something like below;
$username = $_POST['username'];
$password = $_POST['userPassword'];
Then you can use:
$user = $db->$collection->findOne(array('username'=> $username, 'password'=> $password));
Comments
-
Maddy almost 2 years
I am new to mongoDb and PHP. I am trying to create a simple login page which will take the user input from the text fields and look in the database if the record exists.
For now I am able to pass hard coded values which redirects me to a new page but I am unable to query the database.
If someone can guide me in the rite direction. I have looked at various tutorials online but have not been able to get what I need.
<? PHP session_start(); ?> <html> <head> <title> Movie Database </title> </head> <?PHP $_SESSION["loggedInUser"] = $username; try { // open connection to MongoDB server $conn = new Mongo('localhost'); // access database $db = $conn->test; // access collection $collection = $db->items; $userName = $_POST['username']; $userPass = $_POST['userPassword']; $user = $db->$collection->findOne(array('username'=> 'user1', 'password'=> 'pass1')); foreach ($user as $obj) { echo 'Username' . $obj['username']; echo 'password: ' . $obj['password']; if($userName == 'user1' && $userPass == 'pass1'){ echo 'found' } else{ echo 'not found' } } // disconnect from server $conn->close(); } catch (MongoConnectionException $e) { die('Error connecting to MongoDB server'); } catch (MongoException $e) { die('Error: ' . $e->getMessage()); } $_SESSION["loggedInUser"] = $correct; ?> <body> <br> <center><h1> Welcome to CS348 Login Page </h1></center> <br> <form action="page2.php" METHOD="POST"> <label>Username :</label> <input type="text" Name="username"> <br> <label>Password :</label> <input type="password" Name="userPassword"> <br> <br> <input type="submit" value="Login"> <br> </FORM> </body> </html>
I am not sure how to take the userinput from the text boxes and query the database.
Thank you for the help
-
Maddy over 9 yearsHi, thank you for the reply. I did make the changes you suggested and they make more sense. But my webpage is coming out to be blank. Is there a way I can debug that? I have modified the code if you could have a look. Thank you for the help
-
DShah over 9 yearsyou are redirecting your form to page2.php, is this the same page where you have this PHP code?
-
Maddy over 9 yearsI was using page2 to redirect my output to see what values I get. I did figure out the error of not displaying anything ( I was missing semi colons). Should I redirect to the same page?
-
DShah over 9 yearsYes, your php code to get the username and password is on same page, or you can move your PHP code to page2.php
-
Maddy over 9 yearsthank you for that but I just have one last lingering question. The php code to move to next page will be all the if else statements rite?
-
DShah over 9 yearsNo, everything starting from $_SESSION["loggedInUser"] = $username;