如何使用PHP将登录表单错误返回到同一页面?

Cal*_*dan 5 html php forms login

我对PHP比较陌生,并且已经用尽互联网试图找到这个问题的答案.我看过无数的例子,但人们看起来与我的登录系统非常不同,我无法解密它.

到目前为止,这是我的代码:

的index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Video for Education Log In</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>



<body>
<div id="wrapper">
<div id="header">
    <div id="logo">
        videoedu.edu    </div>
    <div id="menu">
        <ul>
            <li><a href="account.html" class="menua">Create Account</a></li>
            <li><a href="about.html" class="menua">About Us</a></li>
        </ul>
    </div>
</div>
<br><br><br><br>
<div id="page">

<div id="content">
    <h2>Video for Education helps you connect and share with the videos in your life.</h2>
    <h3>Upload Share Create Using video for your education purposes. Lecturers Welcome
    Upload Share Create Using video for your education purposes. Lecturers Welcome
    Upload Share Create Using video for your education purposes. Lecturers Welcome</h3>
    <div class= "form">
    <form name="login" method="post" action="checklogin.php">
        Username: <input type="text" name="myusername" id="myusername" class="textb"/><br />
        Password  :  <input type="password" name="mypassword" id="mypassword" class="textb"/><br />
        <br>
        <input type="submit" name="login" value="Login" id="login" class="texta" />
    </form>
    </div>
</div>
</div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

checklogin.php

<?php

$host = "localhost";
$username = "root";
$password = "";
$db_name = "test";
$tbl_name = "members";

mysql_connect("$host", "$username", "$password")or die("Cannot connect.");
mysql_select_db("$db_name")or die("Cannot select DB.");

$myusername=$_POST["myusername"];
$mypassword=$_POST["mypassword"];


    if ($myusername&&$mypassword)
    {

    $myusername = stripslashes($myusername);
    $mypassword = stripslashes($mypassword);
    $myusername = mysql_real_escape_string($myusername);
    $mypassword = mysql_real_escape_string($mypassword);

    $sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
    $result = mysql_query($sql);

    $count = mysql_num_rows($result);

    if($count == 1){
    session_register("myusername");
    session_register("mypassword"); 
    header("location:login_success.php");
        }
    else 
        {
    echo "Wrong Username or Password";
        }
    }

    else

    echo "You have left one or more fields blank.";

?>
Run Code Online (Sandbox Code Playgroud)

login_success.php

<? 
session_start();
if( !isset( $_SESSION['myusername'] ) ){
header("location:account.html");
}

echo "Welcome, ".$_SESSION['myusername']." - You are now logged in.<br>";

echo "<a href=logout.php>Logout</a>"
?>

<html>
<body>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

logout.php

<?php

session_start();

session_destroy();

echo "You have been logged out, <a href='index.php'>click here</a> to return."

?>
Run Code Online (Sandbox Code Playgroud)

我已经尝试将其插入index.html并将文件名更改为index.php.

$submit = $_POST["login"];

if($submit)
{

}
Run Code Online (Sandbox Code Playgroud)

...但它始终会在页面底部不断显示其中一个错误("用户名或密码错误").

我想要它,以便如果用户输入错误的用户名或密码,或将所需字段留空,则错误将弹出在同一页面上,而不是转到一个新的丑陋,空白的PHP页面,顶部显示错误消息左手角落.

Tra*_*ty3 5

在checklogin.php中,不要回显错误,请使用:

die(header("location:index.html?loginFailed=true&reason=password"));
Run Code Online (Sandbox Code Playgroud)

或者类似的东西,在你的index.html页面中,只需让PHP生成HTML消息,如下所示:

    <input type="submit" name="login" value="Login" id="login" class="texta" /><br /><br />
    <?php $reasons = array("password" => "Wrong Username or Password", "blank" => "You have left one or more fields blank."); if ($_GET["loginFailed"]) echo $reasons[$_GET["reason"]]; ?>
</form>
Run Code Online (Sandbox Code Playgroud)

此外,请确保die()exit()当您使用header重定向页面时,否则脚本的其余部分将继续运行.