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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失