I need to check if a username and a password passed by another page are empty but this doesn't work and I can't understand why. Username and password are passed by a form in another page with POST method.
session_start();
if(!(isset($_SESSION['username'])) && !(isset($_SESSION['password']))){
$_SESSION['username']=trim($_POST['username']);
$_SESSION['password']=$_POST['password'];
}
[...]
$username = $_SESSION['username'];
$password = $_SESSION['password'];
if($username=="" || $password=="")
{
echo "<script>
alert('You need to fill all fields!');
window.location.assign('reg.php');
</script>";
}
Thanks.
Edit. Now with this:
if(is_null($username) || is_null($password))
{
echo "<script>
alert('You need to fill all fields!');
window.location.assign('reg.php');
</script>";
}
else
{
if(strlen($username)<5 || strlen($password)<5)
{
echo "<script>
alert('Fiels must be at least of 5 character!');
window.location.assign('reg.php');
</script>";
}
It jumps directly to the second error message, 'Fiels must be at least of 5 character!'. Why? Thanks.
Your now checking if the username and password is empty. $username = ""
But when someone skipped an input it is null. So you have to check that. You can do that with the is_null()
function.
So now you do: is_null($username)
You can check empty(). and you can use header location instead of javascript redirection.
$username = $_SESSION['username'];
$password = $_SESSION['password'];
if(empty($username) || empty($password))
{
header("Location: reg.php");
}