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


How do you return login form errors to the same page using PHP?

我对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>

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.";
?>

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>

logout.php

<?php
session_start();
session_destroy();
echo "You have been logged out, <a href='index.php'>click here</a> to return."
?>

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

$submit = $_POST["login"];
if($submit)
{
}

但它总是在页面底部不断显示其中一个错误("用户名或密码错误")。

我希望这样,如果用户输入了错误的用户名或密码,或者将必填字段留空,错误将在同一页面上弹出,而不是转到一个新的丑陋的空白PHP页面,错误消息在左上角。

在checklogin.php中,不要回显错误,而是使用以下内容:

die(header("location:index.html?loginFailed=true&reason=password"));

或者类似的东西,在你的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>

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

如果数据无效,可以重定向回页面。将错误放入会话并在页面上显示:

例如:

<?php if(isset($_SESSION['Login.Error'])  { echo $_SESSION['Login.Error'];
 unset($_SESSION['Login.Error']); } ?>
<form ....

您的错误将显示在页面上。

在您的PHP 中

 $_SESSION["Login.Error"] = 'Invalid credentials';//redirect back to your login page

在checklogin.php中,如果用户输入了错误的用户名或密码,请使用以下代码:

echo "<script language='"JavaScript'">'n";
echo "alert('Username or Password was incorrect!');'n";
echo "window.location='login.php'";
echo "</script>";

它将在同一页面(登录页面)弹出错误消息,而不是转到空白的PHP页面。

您希望将index.html页面设置为PHP页面,并将表单提交给自己,即index.PHP。通过这种方式,您的索引页面可以对表单值进行登录检查,并适当地显示页面的输出,或者在所有内容都有效的情况下使用标头重定向。

如果没有在完整的上下文中看到它,很难判断你的尝试可能产生的效果,但情况的要点是你需要表单提交给它自己并处理它的登录处理。

看起来您希望/需要将其与jQuery或其他Javascript/AAJAX库集成使事物更体面。jQuery有一个用于表单验证的插件,它很容易集成到您的项目中(显然jQuery库是最低要求)。jQuery站点和jQuery验证插件。您也可以考虑使用像CodeIgniter这样的PHP框架,它也有一个非常有用的表单验证库。CodeIgniter一开始很可怕(就像所有基于MVC的编程库/框架一样),但它是值得的。你可以在netTuts+上观看一些教程。他们创建了一系列名为CodeIgniterFromScratch的教程,不是最新版本,但很容易适应。