Since your code is already checking for missing $username
and $password
data here is what you can do to keep the same code and execution flow
$username = isset($_SESSION['username']) ? $_SESSION['username'] : NULL;
$username = isset($_POST['username']) ? $_POST['username'] : $username;
$password = isset($_POST['password']) ? $_POST['password'] : NULL;
Also, as others have pointed out if you are submitting this $_POST
data from a form you might have to check the $_POST["submitButtonName"]
value as well.