我将所有代码从 mysql 更改为 PDO,但我遇到了一些麻烦。我在这里阅读了很多信息以及 nettus 的教程,但仍然无法弄清楚为什么我的代码不起作用。
下面是 login.php PHP,当我按下登录时,你应该打开我的用户类并开始登录。
<?php
require_once('../global.php');
require_once('../assets/header.php');
if (isLogged())
{
header("Location: $website_domain");
exit;
}
if (isset($_POST['submit']))
{
$username = $_POST['username'];
if(empty($_POST['password']))
{
$password == "";
}
else
{
$password = md5($_POST['password'].$_POST['username']);
}
$object = new User();
$object->Login($username,$password);
}
?>
这是我收到的错误:
Fatal error: Call to a member function prepare() on a non-object in /home/astonish/public_html/labs.site.com/lock/includes/classes/user.class.php on line 32
如果您需要查看我的用户类,请询问,当你们只需要看到错误时,我不想用代码填充它。
用户类.php
<?php
require_once('connection.php');
// new data
$ip = $_SERVER['REMOTE_ADDR'];
$hour = date('h');
$month = date('m');
$date = dateTime();
class User
{
private $db;
public function _construct()
{
$this->db = new Connection();
$this->db = $this->db->dbConnect();
}
public function Login($name, $pass)
{
if(empty($name))
{
echo "Please input a username";
}
elseif(empty($pass))
{
echo "Please input a password";
}
elseif(!empty($name) && !empty($pass))
{
$st = $this->db->prepare("select * from users where username=? and password=?");
$st->bindParam(1, $name);
$st->bindParam(2, $pass);
$st->execute();
if($st->rowCount() == "1")
{
// Delete failed attempts
$st = $db->prepare('DELETE FROM failedLogins WHERE ip = :ip');
$st->bindParam(':ip', $ip); // this time, we'll use the bindParam method
$st->execute();
// Get status of user
$result = $st->fetch(PDO::FETCH_ASSOC);
$result->status;
if ($fetch['status']==1)
{
$error = 'Your account is pending email.';
}
else if ($fetch['status']==2)
{
$_SESSION['main'] = md5(sha1($_SERVER['REMOTE_ADDR'].$_SERVER['HTTP_USER_AGENT'].$fetch['id']));
$_SESSION['uid'] = base64_encode($fetch['id']);
header("Location: $website_domain");
exit;
}
else if ($fetch['status']==5)
{
$error = 'Your account is inactive or was denied.';
}
else
{
$error = 'Invalid account status.';
}
}
}
else
{
$st = $this->db->prepare("INSERT INTO `failedLogins` (ip,hour,month,date) VALUES (:ip,:hour,:month,:date)");
$st->execute(array(':ip'=>$ip, ':hour'=>$hour, ':month'=>$month, ':date'=>$date));
echo 'The username or password entered is invalid.';
}
}
}
?>
连接.php
class Connection
{
public function dbConnect()
{
return new PDO("mysql:host=localhost;dbname=astonish_viral", $mysql_user, $mysql_pass);
/*try
{
$db = new PDO("mysql:host=localhost;dbname=astonish_viral", $mysql_user, $mysql_pass);
echo 'Connected to database';
}
catch(PDOException $e)
{
echo 'ERROR: ' . $e->getMessage();
}*/
}
}
新错误:现在收到以下错误
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO)' in /home/astonish/public_html/labs.site.com/lock/includes/classes/connection.php:12 Stack trace: #0 /home/astonish/public_html/labs.site.com/lock/includes/classes/connection.php(12): PDO->__construct('mysql:host=loca...', NULL, NULL) #1 /home/astonish/public_html/labs.site.com/lock/includes/classes/user.class.php(17): Connection->dbConnect() #2 /home/astonish/public_html/labs.site.com/lock/account/login.php(23): User->__construct() #3 {main} thrown in /home/astonish/public_html/labs.site.com/lock/includes/classes/connection.php on line 12