I am creating an input class with PHP, but the variables used are empty when I pass them to the class method, even if they should not be. I tried to return only the $ username variable, but it is still empty, although if I return it without using the class, I see that it is correctly assigned.
I use several other classes with methods where variables are assigned correctly.
I do not know if I saw myself blind, and I missed something obvious, or if something else caused it.
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));
The form assigns the submitted variables.
<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); $auth->login($username, $password); if ($auth->login()['error']) { echo 'error:' . $auth->login()['message']; } else { echo 'success:' . $auth->login()['message']; } }
EDIT:
If I assign variables to the class method, the code works:
public function login($username = 'user', $password = 'pass')
But if I do this, it will not work:
$username = 'User'; $password = 'pass'; $auth = new Auth($mysqli); $auth->login($username, $password);
Also, if I use the values of $ _POST outside of $ auth-> login (), they are assigned so that they are not empty when passing them to the class ...