douqingzhi0980 2019-02-11 20:36
浏览 88

为什么我的变量是未定义的,des?

User registration works fine. After registration, this login page is opened and the message comes up that password, username and hash are undefined variables on my select line and when I echo the hash. When trying to login "Invalid login credentials" error pops up, but also "Password is valid", and the hash is printed as well.

if (isset($_POST['username']) and isset($_POST['password'])){
// Assigning posted values to variables.
$username = cleanData($_POST['username']);
$password = cleanData($_POST['password']);
$hash = password_hash($password, PASSWORD_DEFAULT);
$verify = password_verify($password, $hash);
    if(password_verify($password, $hash)) {
    echo 'Password is valid!';
} else {
    echo 'Invalid password.';
}
    }

// Checking if the values exist in the database or not
$query = "SELECT * FROM `user` WHERE (username='$username') AND (password='$password')";
echo $hash;
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));
$count = mysqli_num_rows($result);

 // If the posted values are equal to the database values, then session 
is created for the user.
if ($count == 1){
  $_SESSION["loggedIn"] = true;
$_SESSION['username'] = $username;
}else{
// error for not matching credentials
$fmsg = "Invalid Login Credentials.";

I suspect that I have done something horribly wrong with the placement and requirements of my SELECT statement , but it still does not explain why these variables are coming up as undefined. Perhaps I can move the SELECT statement to replace the "password is valid" message and go from there? CleanData is a function to sanitise input.

  • 写回答

2条回答 默认 最新

  • doubi7346 2019-02-11 20:44
    关注

    So, the issue is you're kind of doing this:

    if (username and password are set) {
     username = xxx
     password = xxx
     hash password
     verify password
     display results
    }
    
    do sql query based on username and password
    

    So, you've got code outside of your original IF trying to utilize variables that might not have been created.

    Your SQL query and all code after that will be called even if you did not pass username and password to the php page.

    You might be better off doing something like this at the beginning of your script:

    if (username and password are not both set in post vars)
      display error
      exit
    }
    username = xxx
    password = xxx
    

    Then you can be sure that those vars definitely are defined without having to check over and over in the page.

    Also, the pseudocode is on purpose. Trying to get my message across in layman's terms :)

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)