dongshipang8094 2017-09-22 19:36
浏览 14
已采纳

PHP检查另一个页面传递的空字段

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.

  • 写回答

2条回答 默认 最新

  • doutao1939 2017-09-22 19:39
    关注

    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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?