gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:33:10 25 4
gpt4 key购买 nike

我对 PHP 比较陌生,并且已经用尽了 Internet 试图找到这个问题的答案。我看过无数示例,但人们的登录系统似乎与我的截然不同,我很难理解它。

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

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 - 如何使用 PHP 将登录表单错误返回到同一页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9485059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com