我之前发过一篇关于此事的帖子,并且已经在这个问题上工作了很长一段时间。
我无法将数据从第1页传递到第2页。我已经在线查看了各种教程,但未能获得所需的结果。
我是MongoDB和PHP的新手。 如果有人能解释我如何从mongoDB查询数据。
下面我发布两个页面的代码
//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>
第二页从这里开始
//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());
}
?>
答案 0 :(得分:-1)
以下几点需要注意
检查此链接,您会有所了解......
Simple PHP mongoDB Username and Password Check for site
现在在会话中继续存储用户的ID,然后在任何页面中启动会话并继续使用..
在PHP中,CODE是这样的: -
<?php
session_start();
$_SESSION['user_id'] = $userid;//if match found
?>
在另一个档案中
<?php
session_start();
echo $_SESSION['user_id'];
?>
享受欢呼!!!!!