2014-09-04 53 views
0

我在其他系統中編寫了一個用於登錄php/html的腳本。它與該系統運作良好。現在我將文件導入到另一個系統中。現在,無論何時我打開admin.php腳本,它都會將用戶名分別作爲用戶名和密碼字段中的用戶名和密碼。我無法修復它。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服務器。

的頁面是這樣的:

Page look like this

+0

密碼是自動填入的,是這個問題嗎? – 2014-09-04 15:25:47

+0

@ Fred-ii-兩人都沒有人在工作 – jahan 2014-09-04 15:28:04

+0

@DaveChen是的也是管理員的名字.. – jahan 2014-09-04 15:28:52

回答

1

您的瀏覽器保存和自動填寫用戶名和密碼。

您可以在您的密碼錶單元素中嘗試autocomplete="off",或隨機更改表單元素名稱,但我強烈建議您不要這樣做。如果用戶想要保存他們的憑證,那就是他們的業務。

如果你想在自己的瀏覽器進行調試,但不希望爲您的瀏覽器自動填充,您可以禁用它(鉻,搜索在設置密碼):

+0

在開始的時候, User_username爲空,所以如果(!empty($ user_username)會失敗.. – jahan 2014-09-04 15:35:35

+1

@jahan我猜你的瀏覽器也會自動保存,以及(用戶名和密碼) – 2014-09-04 15:38:31

+0

在私人瀏覽器中,它運行良好。 – jahan 2014-09-04 15:39:36

相關問題