doucaishi0077 2014-09-04 15:21
浏览 93
已采纳

php表单的输入字段显示数据库用户名和密码

I wrote a script for login in php/html in other system. It was working well with that system. Now I imported the files in another system. Now, whenever I open the admin.php script, it gives username as the db username and password of that in username and password field respectively. I am unable to fix it.

Php script:

<?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
}
?>

I am using xampp server.

The page is like this:

Page look like this

  • 写回答

1条回答 默认 最新

  • dongyong1897 2014-09-04 15:33
    关注

    Your browser is saving and auto-completing your username and password.

    You could try autocomplete="off" within your password form element, or randomly change form element names, but I would highly recommend against that. If a user wants to save their credentials, that's their business.

    If you want to debug on your own browser but do not wish for your browser to auto-fill, you can disable it (chrome, search passwords in settings):

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题