我正在修建一个登录系统,我有2个错误:
未定义的变量
和
调用私有方法Database :: connect()
数据库连接文件:
class Database
{
private $host;
private $user;
private $password;
private $database;
function __construct($filename)
{
if(is_file($filename))
include $filename;
else
throw new Exception("Error!");
$this->host = $host;
$this->user = $user;
$this->password = $password;
$this->database = $database;
$this->connect();
}
private function connect()
{
// connect to the server
if(!mysqli_connect($this->host,$this->user, $this->password,$this->database))
throw new Exception("Error: not connected to the server.");
}
function close()
{
mysql_close();
}
}
登录模式:
class Login
{
private $username;
private $password;
private $cxn;
public function __construct($username,$password)
{
$this->setData($username,$password);
$this->connectToDb();
$this->getData();
}
private function setData($username,$password)
{
$this->username = $username;
$this->password = $password;
}
private function connectToDb()
{
include '../model/database.php';
$config = "../model/config.php";
$ok = new Database($config);
$co = $ok->connect();
}
public function getData()
{
$query = "SELECT * FROM admin
WHERE
'username' = '$this->username'
AND
'password' = '$this->password'";
$sql = mysqli_query($ok,$query);
if (mysqli_num_rows($sql) > 0)
{
return TRUE;
}
else
{
throw new Exception("Error Processing Request");
}
}
public function close()
{
$this->cxn->close();
}
}
登录控制器:
include '../view/login.php';
if (isset($_POST['sub']))
{
$_POST['user'] = $username;
$_POST['pass'] = $password;
try
{
include'../model/login.php';
$login = new login($username,$password);
if ($login == TRUE) {
session_start();
$_SESSION['username'] = $username;
header('Location:../view/index.php');
}
} catch (Exception $e) {
echo $e->getMessage();
}
}