dongzhui2636 2019-03-04 18:01
浏览 69

点击登录按钮后页面没有变化?

creating a log in page using php and PDO however once I fill out the form with the correct username and password to log into the system and click log in, nothing changes except the boxes are now empty as if I never entered a username and password, can someone advise me as to what is going wrong?

this is the php code:

<?php

session_start();
$server = "127.0.0.1";
$dbusername = "root";
$dbpassword = "";
$db = "movie1";
$message = "";
try
{
    $handle = new PDO("mysql:host=$server; dbname=$db", $dbusername, $dbpassword);
    $handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    if(isset($_POST["login"]))
    {
        if(empty($_POST["username"]) || empty($_POST["password"]))
        {
            $message = '<label>All fields are required</label>';
        }
        else
        {
            $query = "SELECT * FROM register WHERE username = :username AND password = :password";
            $statement = $handle->prepare($query);
            $statement->execute(
                array(
                    'username'     =>     $_POST["username"],
                    'password'     =>     $_POST["password"]
                )
            );
            $count = $statement->rowCount();
            if($count > 0)
            {
                $_SESSION["username"] = $_POST["username"];
                header("location:login_success.php");
            }
            else
            {
                $message = 'Wrong Data';
            }
        }
    }
}
catch(PDOException $error)
{
    $message = $error->getMessage();
}
?>

and this is the html form to which it applies to:

<!DOCTYPE html>
<html lang="en">

<meta charset="UTF-8">
<title>log in</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<body>

<form class="w3-container w3-card-4" action="login.php" method="post">
    <h2 class="w3-text-black">Log in</h2>
    <?php
    if(isset($message))
    {
        echo '<label class "text-danger">'.$message.'</label>';
    }
        ?>

    <p>
        <label class="w3-text-black"><b>username</b></label>
        <input class="w3-input w3-border" name="username" type="text" placeholder="username"></p>
    <p>
        <label class="w3-text-black"><b>Password</b></label>
        <input class="w3-input w3-border" name="password" type="text" placeholder="********"></p>
    <p>
        <input type="submit" name="login" class="w3-btn w3-black">Log in</input>
    </p>

    <p>

please ignore that passwords are not hashed, I will fix that later. the first image is what the log in form looks like with information entered and the next will be what happens after I click log in (pic one)1

pic two 2

  • 写回答

1条回答 默认 最新

  • doudou8893 2019-03-07 18:13
    关注

    I have made only minor changes to get your script working, see comments below. I think your login_success.php sends the authenticated user back to the login page.

    <?php
    /* my table in db named 'movie1':
    
    CREATE TABLE `register` (
      `ID` int(11) NOT NULL  AUTO_INCREMENT  PRIMARY KEY,
      `username` varchar(100) NOT NULL,
      `password` varchar(100) NOT NULL
    );
    
    INSERT INTO `register` (`username`, `password`) VALUES
    ('User1', '1111'),
    ('User2', '2222');
    
    */
    
    session_start();
    $server = "127.0.0.1";
    $dbusername = "www"; //Changed
    $dbpassword = "www"; //Changed
    $db = "movie1";
    $message = "";
    try
    {
        $handle = new PDO("mysql:host=$server; dbname=$db", $dbusername, $dbpassword);
        $handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        if(isset($_POST["login"]))
        {
            if(empty($_POST["username"]) || empty($_POST["password"]))
                $message = '<label>All fields are required</label>';
            else
            {
                $query = "SELECT * FROM register WHERE username = :username AND password = :password";
                $statement = $handle->prepare($query);
                $statement->execute(
                    array(
                        ':username'     =>     $_POST["username"], //Inserted colon here
                        ':password'     =>     $_POST["password"]  //Inserted colon here
                    )
                );
                $count = $statement->rowCount();
                if($count > 0)
                {
                    $_SESSION["username"] = $_POST["username"];
                    header("location:FileNotExistAndWillProduce404WhichMeansSuccess.php");//Target changed
                }
                else
                    $message = 'Wrong Data';
            }
        }
    }
    catch(PDOException $error)
    {
        $message = $error->getMessage();
    }
    ?>
    <!DOCTYPE html>
    <html lang="en">
    
    <meta charset="UTF-8">
    <title>log in</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
    <body>
    
    <form class="w3-container w3-card-4" action="LoginTest.php" method="post"> <!-- action-target changed -->
        <h2 class="w3-text-black">Log in</h2>
        <?php
        if($message>'') //was isset(), changed, because it contains at least an empty string
            echo '<label class "text-danger">'.$message.'</label>';
        ?>
    
        <p>
            <label class="w3-text-black"><b>username</b></label>
            <input class="w3-input w3-border" name="username" type="text" placeholder="username"></p>
        <p>
            <label class="w3-text-black"><b>Password</b></label>
            <input class="w3-input w3-border" name="password" type="text" placeholder="********"></p>
        <p>
            <input type="submit" name="login" class="w3-btn w3-black">Log in</input>
        </p>
    
    </form></body></html>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧