dongqian1028 2018-05-20 12:38
浏览 65
已采纳

PHP登录表单不起作用,我该怎么办才能修复它?

I have the following two php scripts:
config.php:

<?php
define('DB_SERVER', 'localhost:3306');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'mind3');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>

and index.php, which contains the login script and form:

    <?php
require_once 'config.php';
$username=$password='';
$username_err=$password_err='';
if($_SERVER["REQUEST_METHOD"] == "POST"){

    if(empty(trim($_POST["username"]))){
    $username_err = 'Please enter an username!';
    } else{
    $password = trim($_POST['username']);  
    }

    if(empty(trim($_POST["password"]))){
    $pasword_err = "Please enter a password!";
    } else{
    $password = trim($_POST['password']);
    }

    if(empty($username_err) && empty($password_err)){
    $sql = "SELECT username,password FROM members WHERE username = ?";

    if($stmt = mysqli_prepare($link, $sql)){
        mysqli_stmt_bind_param($stmt, "s", $param_username);
        $param_username = $username;
        if(mysqli_stmt_execute($stmt)){
        mysqli_stmt_store_result($stmt);
        if(mysqli_stmt_num_rows($stmt) == 1){
            if(password_verify($password, $hashed_password)){
            session_start();
            $_SESSION['username'] = $username;
            header("location: welcome.php");

            } else{
            $username_err = 'Username/password is wrong!';
        }
        } else{
        echo "Oops! Something went wrong. Please try again later.";
        }
    }
    mysqli_stmt_close($stmt);
    } 
    mysqli_close($link);
} 
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <div class="wrapper">
    <h2>Log in</h2>
    <form action="<?php echo htmlspecialchars ($_SERVER["PHP_SELF"]); ?>" method="post">
        <div class="form-group <?php echo(!empty($username_err)) ? 'has-error' : ''; ?>">
            <input type="text" name="username" class="form-control" placeholder="Username">
            <span class="help-block"><?php echo $username_err; ?></span>
        </div>
        <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
            <input type="password" name="password" class="form-control">
            <span class="help-block"><?php echo $password_err; ?></span>
        </div>
        <div class="form-group">
            <input type="submit" class="btn btn-primary" value="Login">
        </div>
    </form>
    </div>
</body>
</html>

The problem is that whenever I input the credentials I have in my database, it does not redirect me to welcome.php, and it also doesn't tell me that the password or username is wrong.But when i don't input username or password I do get "please enter username" or "please enter password".

I don't understand what is wrong.

  • 写回答

3条回答 默认 最新

  • doume5227 2018-05-20 12:55
    关注

    You're not setting the value of $username correctly.

    Change:

    $password = trim($_POST['username']); 
    

    To:

    $username = trim($_POST['username']);
    

    $hashed_password has to have a value since it's needed in password_verify(). Used mysqli's bind_result and fetch methods to get the value needed.

    After:

    if(mysqli_stmt_num_rows($stmt) == 1){
    

    Add:

    $hashed_password = "";
    $username2 = "";
    
    mysqli_stmt_bind_result($stmt, $username2, $hashed_password);
    mysqli_stmt_fetch($stmt);
    

    Added exit(); after header(..) redirect so no other codes will be executed after the redirect call.

    Updated Code:

    <?php
    require_once 'config.php';
    $username = $password = '';
    $username_err = $password_err = '';
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
        if (empty(trim($_POST["username"]))){
            $username_err = 'Please enter an username!';
        }
        else {
            $username = trim($_POST['username']);
        }
    
        if (empty(trim($_POST["password"]))){
            $pasword_err = "Please enter a password!";
        }
        else {
            $password = trim($_POST['password']);
        }
    
        if (empty($username_err) && empty($password_err)) {
            $sql = "SELECT username,password FROM members WHERE username = ?";
    
            if ($stmt = mysqli_prepare($link, $sql)) {
                mysqli_stmt_bind_param($stmt, "s", $param_username);
                $param_username = $username;
                if (mysqli_stmt_execute($stmt)){
                    mysqli_stmt_store_result($stmt);
                    if (mysqli_stmt_num_rows($stmt) == 1){
                        $hashed_password = "";
                        $username2 = "";
    
                        mysqli_stmt_bind_result($stmt, $username2, $hashed_password);
                        mysqli_stmt_fetch($stmt);
                        if (password_verify($password, $hashed_password)){
    
                            $_SESSION['username'] = $username;
                            mysqli_stmt_close($stmt);
                            mysqli_close($link);
                            header("location: welcome.php"); exit();
    
                        }
                        else{
                            $username_err = 'Username/password is wrong!';
                        }
                    }
                    else {
                        echo "Oops! Something went wrong. Please try again later.";
                    }
                }
                mysqli_stmt_close($stmt);
            }
            mysqli_close($link);
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,