php 表单的输入字段显示数据库用户名和密码
Posted
技术标签:
【中文标题】php 表单的输入字段显示数据库用户名和密码【英文标题】:The input field of a php form is showing the database username and password 【发布时间】:2014-10-29 10:14:14 【问题描述】:我在其他系统的php/html中编写了一个登录脚本。它与该系统运行良好。现在我将文件导入另一个系统。现在,每当我打开 admin.php 脚本时,它会分别在用户名和密码字段中将用户名作为数据库用户名和密码。我无法修复它。
PHP 脚本:
<?php
session_start();
$error_msg = "";
// If the admin isn't logged in, try to log them in
if (!isset($_SESSION['a_id']))
if (isset($_POST['submit']))
// Connect to the database
require_once('connectvars.php');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Grab the user-entered log-in data
$user_username = mysqli_real_escape_string($dbc, trim($_POST['username']));
$user_password = mysqli_real_escape_string($dbc, trim($_POST['password']));
if (!empty($user_username) && !empty($user_password))
// Look up the username and password in the database
$query = "SELECT a_id, a_name FROM admin WHERE a_name = '$user_username' AND a_password = '$user_password'";
$data = mysqli_query($dbc, $query);
if (mysqli_num_rows($data) == 1)
// The log-in is OK so set the user ID and username session vars (and cookies), and redirect to the home page
$row = mysqli_fetch_array($data);
$_SESSION['a_id'] = $row['a_id'];
$_SESSION['a_name'] = $row['a_name'];
setcookie('a_id', $row['a_id'], time() + (60 * 60 * 24 * 30)); // expires in 30 days
setcookie('a_name', $row['a_name'], time() + (60 * 60 * 24 * 30)); // expires in 30 days
// $home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php';
// header('Location: ' . $home_url);
else
// The username/password are incorrect so set an error message
$error_msg = 'Sorry, you must enter a valid username and password to log in.';
else
// The username/password weren't entered so set an error message
$error_msg = 'Sorry, you must enter your username and password to log in.';
echo "You need to log in.";
?>
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Log In Admin</title>
<style>
label
display: inline-block;
width: 140px;
</style>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h3> Log In Admin</h3>
<?php
// If the session var is empty, show any error message and the log-in form; otherwise confirm the log-in
if (empty($_SESSION['a_id']))
echo '<p class="error">' . $error_msg . '</p>';
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend>Log In</legend>
<label for="username">Admin Name:</label>
<input type="text" name="username" value="<?php if (!empty($user_username)) echo $user_username; ?>" /><br />
<label for="password">Password:</label>
<input type="password" name="password" />
</fieldset>
<input type="submit" value="Log In" name="submit" />
</form>
<p> It is log in page for admin. If you are user and want to log in , click <a href="login.php"> here </a> </p>
<?php
else
// Confirm the successful log-in
echo'You are logged in as ' . $_SESSION['a_name'] ;
?>
</body>
</html>
<html>
<head>
<title>
Admin Page
</title>
</head>
<body>
<h5> What do you want </h5>
<a href="question_records.php"> Manage Questions</a><br>
<a href="user_records.php"> Manage Users </a><br>
<a href="logout_admin.php"> Log Out </a>
</body>
</html>
<?php
?>
我正在使用 xampp 服务器。
页面是这样的:
【问题讨论】:
密码是自动填的,是这个问题吗? @Fred-ii- 两者都没有工作 @DaveChen 是的,还有管理员名称.. 不,事实并非如此......两者都是不同的......正如你在我的脚本中看到的那样 【参考方案1】:您的浏览器正在保存并自动填写您的用户名和密码。
您可以在密码表单元素中尝试autocomplete="off"
,或随机更改表单元素名称,但我强烈建议您不要这样做。如果用户想要保存他们的凭据,那是他们的事。
如果您想在自己的浏览器上调试但不希望浏览器自动填充,您可以禁用它(chrome,在设置中搜索密码):
【讨论】:
一开始 User_username 是空的,所以 if(!empty($user_username) 会失败.. @jahan 我猜你的浏览器也会自动保存它。 (用户名和密码) 谢谢先生,问题出在自动保存的密码上。以上是关于php 表单的输入字段显示数据库用户名和密码的主要内容,如果未能解决你的问题,请参考以下文章