0

I had made a previous post regarding this and have been working on this issue for quite a while.

I am unable to pass data from page1 to page2. I have looked at various tutorials online but have not been able to get the desired result.

I am new to both MongoDB and PHP. If someone could explain how I can query data from mongoDB.

Below I am posting the code for both pages

//LOGIN PAGE <? PHP session_start(); ?> <html> <head> <title> Movie Database </title> </head> <?PHP try { // open connection to MongoDB server $conn = new Mongo('localhost'); // access database $db = $conn->test; // access collection $collection = $db->items; // execute query $cursor = $collection->find(); foreach ($cursor as $obj) { $correct = $obj['username']; } // disconnect from server $conn->close(); } catch (MongoConnectionException $e) { die('Error connecting to MongoDB server'); } catch (MongoException $e) { die('Error: ' . $e->getMessage()); } ?> <body> <br> <center> <h1> Welcome to Login Page </h1> <br> <br> <br> <form action="page.php" METHOD="POST"> <label>Username :</label> <input type="text" Name="username"> <br> <label>Password :</label> <input type="password" Name="password"> <br> <br> <input type="submit" value="Login"> <br> </center> </FORM> </body> </html> 

Second Page starts from here

 //PAGE 2 <?PHP session_start(); //$db = $conn->test; //echo 'Username: ' . $_POST['username'] . '<br>'; //echo 'Password: ' . $_POST['password'] . '<br>'; $userName = $_POST['username']; $userPass = $_POST['password']; $collection = $db->items; $userN = array('username'=>$userName); $userP = array('password'=>$userPass); $cursor = $collection->find(array($userN,$userP)); foreach ($cursor as $obj) { $foundUserN = $obj['username']; $foundUserP = $obj['password']; if($obj['username'] == $userName && $obj['password'] == $userPass){ echo '<h1>FOUND</h1>'; } else{ echo 'not found'; } } $conn->close(); try { $conn = new Mongo('localhost'); $db = $conn->test; $conn->close(); } catch (MongoConnectionException $e) { die('Error connecting to MongoDB server'); } catch (MongoException $e) { die('Error: ' . $e->getMessage()); } ?> 
1
  • You should be hashing your passwords! Commented Dec 3, 2014 at 9:31

1 Answer 1

-1

Here are few points to note

  1. Session not being used(simply started).
  2. collecting information before establishing db connection.(returns NULL)

Check this link you will get some idea...

Simple PHP mongoDB Username and Password Check for site

Now go ahead store user's id in session and then in any page start the session and keep using..

In PHP CODE goes like this:-

 <?php session_start(); $_SESSION['user_id'] = $userid;//if match found ?> 

In another file

 <?php session_start(); echo $_SESSION['user_id']; ?> 

enjoy cheers!!!!!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.