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条)

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟