Php 未传递给类方法的变量
我正在用PHP构建一个登录类,但是当我将它们传递给class方法时,使用的变量是空的,即使它们不应该是空的。 我尝试只返回$username变量,但它仍然是空的,尽管如果我不使用类返回它,我可以看到它被正确分配 我正在使用多个其他类和方法,在这些方法中变量被正确分配 我不知道我是不是睁大眼睛看不见自己,错过了一些明显的东西,或者是其他什么原因造成的Php 未传递给类方法的变量,php,class,methods,Php,Class,Methods,我正在用PHP构建一个登录类,但是当我将它们传递给class方法时,使用的变量是空的,即使它们不应该是空的。 我尝试只返回$username变量,但它仍然是空的,尽管如果我不使用类返回它,我可以看到它被正确分配 我正在使用多个其他类和方法,在这些方法中变量被正确分配 我不知道我是不是睁大眼睛看不见自己,错过了一些明显的东西,或者是其他什么原因造成的 class Auth { private $mysqli; public function __construct(mysqli
class Auth
{
private $mysqli;
public function __construct(mysqli $mysqli)
{
$this->mysqli = $mysqli;
}
public function login($username, $password) //These variables are empty, even when they shouldn't be
{
$return['error'] = true;
$uid = $this->getUserId(strtolower($username)); //Returns false because $username variable is empty
if (!$uid) {
$return['message'] = 'No such user.'; //Output
return $return;
}
$user = $this->getUser($uid);
if (!password_verify($password, $user['password'])) {
$return['message'] = 'Password incorrect';
return $return;
}
$return['error'] = false;
$return['message'] = 'Logged in';
return $return;
}
private function getUserId($username)
{
$stmt = $this->mysqli->prepare("SELECT id FROM users WHERE username = ?");
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id);
if ($stmt->num_rows < 1) {
return false;
}
$stmt->fetch();
return $id;
}
private function getUser($uid)
{
$stmt = $this->mysqli->prepare("SELECT username, password, email FROM users WHERE id = ?");
$stmt->bind_param('s', $uid);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($username, $password, $email);
if ($stmt->num_rows < 1) {
return false;
}
$stmt->fetch();
$return['uid'] = $uid;
$return['username'] = $username;
$return['password'] = $password;
$return['email'] = $email;
return $return;
}
}
但如果我这样做,它将不起作用:
$username = 'User';
$password = 'pass';
$auth = new Auth($mysqli);
$auth->login($username, $password);
另外,如果我使用$auth->login()之外的$_POST值,它们被分配,因此在将它们传递给类时它们不是空的…问题似乎是您不存储结果,而是再次调用login而不使用值:
如果($auth->login()['error']){
试试这个:
<form method="POST" action="post.php">
<label>Username
<input style="display:block;width:250px;" type="text" name="username" required></label>
<label>Password
<input style="display:block;width:250px;" type="password" name="password"></label>
<button style="display:block;" class="default_btn">Log in</button>
</form>
if (isset($_POST['username'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$auth = new Auth($mysqli);
$login_result = $auth->login($username, $password);
if ($login_result['error']) {
echo 'error:' . $login_result['message'];
} else {
echo 'success:' . $login_result['message'];
}
}
用户名
密码
登录
如果(isset($_POST['username'])){
$username=$_POST['username'];
$password=$_POST['password'];
$auth=newauth($mysqli);
$login\u result=$auth->login($username,$password);
if($login_result['error'])){
回显“错误:”.$login_结果['message'];
}否则{
回显“成功:”。$login_结果['message'];
}
}
如果在调用$auth->login
之前执行var\u dump
操作,然后在方法中做第一件事,你会得到什么?也许$\u POST是空的。我很快检查了它,我的测试变量就通过了。我99%确定POST变量是空的,这里是“从username='$username'的用户中选择id”
将变量直接注入sql字符串(=>手工构造)。这样做与不使用准备好的语句一样危险。相反,您应该执行类似于“从username=:username
和更高版本的$stmt->execute(array('username'=>$username));
的操作。放置变量转储($username、$password、\uuuuuuuuu文件\uuuuuuuuu行\uuuuuuuuu)
作为Auth::login
函数的第一行。这将确保使用正确的类。@RyanVincent我使用的是PHP5.6.10。谢谢,我很好奇你会得到什么结果。是的!这样做了,我不敢相信我自己没有想到,但我猜当你一直盯着自己看的时候会发生这种情况.非常感谢!
$username = 'User';
$password = 'pass';
$auth = new Auth($mysqli);
$auth->login($username, $password);
<form method="POST" action="post.php">
<label>Username
<input style="display:block;width:250px;" type="text" name="username" required></label>
<label>Password
<input style="display:block;width:250px;" type="password" name="password"></label>
<button style="display:block;" class="default_btn">Log in</button>
</form>
if (isset($_POST['username'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$auth = new Auth($mysqli);
$login_result = $auth->login($username, $password);
if ($login_result['error']) {
echo 'error:' . $login_result['message'];
} else {
echo 'success:' . $login_result['message'];
}
}