2013-03-03 98 views
0

我有一個登錄表單(HTML - > PHP)。我輸入正確的細節,然後重新加載頁面,我必須再次輸入細節。我按提交,然後再次執行。在第三次(有時我認爲第二次)它實際上使我登錄。登錄表格需要在登錄前完成若干次

任何幫助將不勝感激。

這裏是HTML表單:

<!-- Login Form --> 
        <form method="post" id="login-form-splash"> 
        <input type="text" class="text" name="username" onfocus="if(this.value == 'Username') { this.value = ''; }" value="Username" /> 
        <input type="password" name="password" class="password" onfocus="if(this.value == 'Password') { this.value = ''; }" value="Password" /> 
        <input type="submit" name="submit" value="Login" class="submit" /> 
        <br /> 
        <a href="resetpassword.php"><span>Lost Password?</span></a> 
        <br /> 
        No account yet? <a href="register.php">Register</a>.<br /> 
      </form> 

這裏是PHP實際上做的登錄:

<?php 
//Check if login form was submitted. 
if(isset($_POST['submit'])) 
{ 

    //include important settings and functions. 
    include($_SERVER['DOCUMENT_ROOT'] . '/includes/config.php'); 

    //Check if both fields were completed. 
    if($_POST['username'] == '' || $_POST['password'] == '') 
    { 
     //Tell them whats wrong. 
     echo "<script language=\"javascript\">alert('You need to complete both fields.');</script>"; 
    } else 
     { 
      //The completed both fields. 
      //Localise vars. 
      $username = $_POST['username']; 
      $password = $_POST['password']; 

      //Protect against SQL Injection using mysql_prep function. [mysql_prep can be found in ./includes/functions.php] 
      mysql_prep($username); 
      mysql_prep($password); 

      //MD5 Hash the password to check against hashed password in DB. 
      $password = md5($password); 

      //Connect to MySQL Database. 
      include($_SERVER['DOCUMENT_ROOT'] . '/includes/connect.php'); 

      //If connection exists 
      if(isset($mysql_connection)) 
      { 
       //Run MySQL Query on DB. 
       $sql = "SELECT * FROM `users` WHERE `username` = '$username' AND `password` = '$password'"; 
       $result = mysql_query($sql, $mysql_connection) or die('Cannot Execute:'. mysql_error()); 

       //Check if there is a match. There can only be one. 
       if(mysql_num_rows($result) == 1) 
       { 
        //Create session variables and set values within them. 
        $_SESSION['username'] = $username; 

        //Redirect to Members page. 
        header('Location: members.php'); 
       } else 
        { 
         //Username and Password are not correct, or the account doesn't exist. 
         echo "<script language=\"javascript\">alert('Please check that you have entered the details correctly.');</script>"; 
        } 
      } else 
       { 
        //Database error. 
        echo "<script language=\"javascript\">alert('There was a database error. Please try again or contact a technician at [email protected]');</script>"; 
       } 
     } 
} 
?> 
+0

mysql_prep是否返回任何內容/它有什麼作用?你可能想在你的頭後添加一個'exit;'。 – Class 2013-03-03 09:36:58

+1

也許您在實際登錄之前檢查用戶是否已登錄?例如:「檢查用戶是否已登錄 - >設置變量$ userIsLoggedIn = false - >處理表單請求 - >解析頁面」。該錯誤將需要第二次頁面加載才能實際識別用戶登錄。 – 2013-03-03 09:55:05

+0

另一件事:會話是否正常工作?嘗試在每個頁面文件的開頭使用session_start()。 – 2013-03-03 09:59:37

回答

0

也許你應該更換

if(isset($mysql_connection)) { 

只是

if($mysql_connection){