所以我正在为我正在开发的网络应用程序创建一个登录系统。我有一个register.php
文件,它不是一个类。我有一个AccountInterface.php
文件,这是一个类。
register.php
<?php
session_start();
if(isset($_SESSION["loggedin"]))
{
header("http://ezblog.guru");
return;
}
require_once("../api/AccountInterface.php");
require_once("../api/Utils.php");
use Massively\api\AccountInterface;
$accountAPI = new AccountInterface();
$creds = parse_ini_file("../../config/mysql.ini");
$conn = new mysqli($creds["ip"], $creds["username"], $creds["password"], $creds["db"]);
var_dump($conn);
if($conn->connect_error)
{
echo "MySQLi connect error:" . $conn->connect_error;
} else
{
$username = $_POST["username"];
$email = $_POST["email"];
$password = $_POST["password"];
if(filter_var($email, FILTER_VALIDATE_EMAIL) === $email)
{
if(!filter_var($username, FILTER_VALIDATE_EMAIL))
{
if(!$accountAPI->checkAccount($username, $conn))
{
if(!$accountAPI->accountExists($username, $email, $conn))
{
$ip = $_SERVER['REMOTE_ADDR']?:($_SERVER['HTTP_X_FORWARDED_FOR']?:$_SERVER['HTTP_CLIENT_IP']);
$salt = Massively\api\Utils::random_str(40);
$pin = rand(10000, 99999);
$accountAPI->registerAccount($username, $email, hash_pbkdf2("sha512", $password, $salt, 27000), $ip, $salt, $pin, $conn);
$msg = "$username,\nThank you for registering an account with E-Z Blog. Before you get started writing blogs using our beautiful web application, please confirm your e-mail by visiting ezblog.guru/confirm.\nYour pin code is $pin\n/E-Z Blog";
$msg = wordwrap($msg);
$headers = "From: ezblog@gmail.com <E-Z Blog>";
//mail($email, "EZ-Blog - confirmation", $msg, $headers);
return;
} else
{
echo "This Email address is already taken";
return;
}
} else
{
echo "This username is already taken";
return;
}
} else
{
echo "Please make sure your username is not an e-mail address";
return;
}
} else
{
echo "Please enter a valid Email address";
return;
}
}
?>
AccountInterface.php
<?php
namespace Massively\api;
class AccountInterface
{
public function checkAccount($user, $conn)
{
if($conn instanceof mysqli)
{
$stmt = $conn->prepare("SELECT id FROM accounts WHERE username=?;");
$stmt->bind_param("s", $user);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows === 0)
{
$stmt->close();
return false;
} else
{
$stmt->close();
return true;
}
} else
{
return "!instanceof mysqli";
}
}
public function accountExists($user, $email, $conn)
{
if($conn instanceof mysqli)
{
$stmt = $conn->prepare("SELECT id FROM accounts WHERE username=?;");
$stmt->bind_param("s", $user);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows !== 0)
{
$foo = "!user";
} else
{
$stmt->free_result();
$stmt->close();
$stmt = $conn->prepare("SELECT id FROM accounts WHERE email=?;");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows !== 0)
{
$foo = "!email";
} else
{
$foo = false;
}
}
$stmt->free_result();
$stmt->close();
return $foo;
} else
{
return "!instanceof mysqli";
}
}
public function getUser($email, $conn)
{
if($conn instanceof mysqli)
{
$stmt = $conn->prepare("SELECT username FROM accounts WHERE email=?;");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows === 0)
{
$stmt->free_result();
$stmt->close();
return false;
} else
{
$stmt->bind_result($user);
while($stmt->fetch())
{
$foo = $user;
}
$stmt->free_result();
$stmt->close();
return $foo;
}
} else
{
return "!instanceof mysqli";
}
}
public function registerAccount($user, $email, $password, $ip, $salt, $pin, $conn)
{
if($conn instanceof mysqli)
{
if(!$this->accountExists($user, $email, $conn))
{
$stmt = $conn->prepare("INSERT INTO accounts (username, email, password, ip, salt, pin, verified) VALUES (?, ?, ?, ?, ?, ?, ?);");
$stmt->bind_param("sssssii", $user, $email, $password, $ip, $salt, $pin, 0);
$stmt->execute();
$stmt->close();
return true;
} else
{
return false;
}
} else
{
return "!instanceof mysqli";
}
}
public function unregisterAccount($user, $conn)
{
if($conn instanceof mysqli)
{
if($this->checkAccount($user, $conn))
{
$stmt = $conn->prepare("DELETE FROM accounts WHERE username=?;");
$stmt->bind_param("s", $user);
$stmt->execute();
$stmt->close();
return true;
} else
{
return false;
}
} else
{
return "!instanceof mysqli";
}
}
public function getPin($user, $conn)
{
if($conn instanceof mysqli)
{
if($this->checkAccount($user, $conn))
{
$stmt = $conn->prepare("SELECT pin FROM accounts WHERE username=?;");
$stmt->bind_param("s", $user);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($pin);
while($stmt->fetch())
{
$foo = $pin;
}
$stmt->free_result();
$stmt->close();
return $pin;
} else
{
return false;
}
} else
{
return "!instanceof mysqli";
}
}
public function login($user, $password, $conn)
{
if($conn instanceof mysqli)
{
if($this->checkAccount($user, $conn))
{
$stmt = $conn->prepare("SELECT id FROM accounts WHERE username=? AND password=?;");
$stmt->bind_param("ss", $user, $password);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows === 0)
{
$foo = "allowed";
} else
{
$foo = "!allowed";
}
$stmt->free_result();
$stmt->close();
return $foo;
} else
{
return false;
}
} else
{
return "!instanceof mysqli";
}
}
public function getSalt($user, $conn)
{
if($conn instanceof mysqli)
{
if($this->checkAccount($user, $conn))
{
$stmt = $conn->prepare("SELECT salt FROM accounts WHERE username=?;");
$stmt->bind_param("s", $user);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($salt);
while($stmt->fetch())
{
$foo = $salt;
}
$stmt->free_result();
$stmt->close();
return $foo;
} else
{
return false;
}
} else
{
return "!instanceof mysqli";
}
}
public function setVerified($user, $conn)
{
if($conn instanceof mysqli)
{
if($this->checkAccount($user, $conn))
{
$stmt = $conn->prepare("UPDATE accounts SET verified=? WHERE username=?;");
$stmt->bind_param("is", 1, $user);
$stmt->execute();
$stmt->close();
return true;
} else
{
return false;
}
} else
{
return "!instanceof mysqli";
}
}
public function getVerified($user, $conn)
{
if($conn instanceof mysqli)
{
if($this->checkAccount($user, $conn))
{
$stmt = $conn->prepare("SELECT verified FROM accounts WHERE username=?");
$stmt->bind_param("s", $user);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($verified);
while($stmt->fetch())
{
$foo = $verified;
}
$stmt->free_result();
$stmt->close();
return $foo;
} else
{
return false;
}
} else
{
return "!instanceof mysqli";
}
}
public function getUserObject($user, $conn)
{
if($conn instanceof mysqli)
{
if($this->checkAccount($user, $conn))
{
$stmt = $conn->prepare("SELECT id, email, ip FROM accounts WHERE username=?;");
$stmt->bind_param("s", $user);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id, $email, $ip);
while($stmt->fetch())
{
$foo = new Account($id, $user, $email, $ip);
}
$stmt->free_result();
$stmt->close();
return $foo;
} else
{
return false;
}
} else
{
return "!instanceof mysqli";
}
}
}
?>
我用PHP调用的所有函数都返回“!instanceof mysqli”,即使我删除了if($conn instanceof mysqli){}
,db也没有任何反应。但是,如果我在var_dump($conn)
中的任何位置添加register.php
,我会认为它是mysqli
对象。我假设$conn
未正确传递给AccountInterface
函数?我该怎么做才能解决这个问题?
我知道$accountAPI->checkAccount($username, $conn)
会返回"!instanceof mysqli"
,因为我添加了if($accountAPI->checkAccount($username, $conn) === "!instanceof mysqli"){ echo "not an instance of mysqli"; }
,其中输出了not an instance of mysqli
。我没有在这里展示,因为我想展示我的原始代码。
在使用checkAccount
函数稍微烦躁不安后,我添加了var_dump($stmt)
,结果$stmt
是mysqli_stmt
的一个实例,但没有做任何事情数据库仍在。
答案 0 :(得分:-1)
您可以通过实际应用依赖注入来解决它。您的代码看起来有点像这样:
namespace Massively\api;
use MySQLi;
class Authentication
{
private $connection;
public function __construct(MySQLi $connection)
{
$this->connection = $connection;
}
public function checkAccount($user)
{
$sql = 'SELECT id FROM accounts WHERE username=?';
$stmt = $this->connection->prepare($sql);
// ... etc.
在register.php
中,它看起来像是:
$conn = new \MySQLi($creds["ip"], $creds["username"], $creds["password"], $creds["db"]);
$accountAPI = new \Massively\api\Authentication($conn);
// some unimportant code here
if(!$accountAPI->checkAccount($username)) {
if(!$accountAPI->accountExists($username, $email))
{
// more code here
}
}
你真的需要努力命名。