2012-11-09 57 views
0

我正在從數據庫中提取記錄以匹配用戶輸入的網站上工作,如果數據與數據庫中的詳細信息不匹配,它必須顯示一個JavaScript警告框。但問題是JavaScript框也顯示身體負荷。以下是我的代碼:在php中使用javascript警報

//Storing the value of form's username and password into PHP variables 
$username = $_POST['username']; 
$password = $_POST['password']; 
//This query is to check whether ther user has provided valid details 
$query = mysql_query("Select * from accounts where username='$username' and  password='$password'"); 
$status = mysql_num_rows($query); 
/**Now checking whether the number of rows returned by database are greater than 0 to verify the login 
if number of rows are greater than zero we will redirect the user to index page with a  session variable**/ 

if($status >0) 
{ 
$_SESSION['username'] = $username; 
?> 
<meta http-equiv="refresh" content="0;url=index.php"> 
<?} 
else 
{?> 
<script language="javascript"> 
alert('UserName or Password does not match. Please try again'); 
</script> 
<?}?> 
+2

的java!=的JavaScript。 –

+5

**警告**您的代碼極易遭受SQL注入攻擊。也應該**從不**將用戶密碼存儲在數據庫中。 –

+1

@Jessica布朗尼完成丹尼爾說的話,我建議你尋找密碼散列(並且永遠不要只散列密碼,總是包含鹽和其他部分,如用戶名,如果它是恆定的)。 –

回答

0

您需要一個登錄標誌。

添加到您的形式:

<input type="hidden" name="login_process" value="true" /> 

,然後圍繞您的登錄代碼:

if($_POST["login_process"] == "true"){ 

    //your login code 

} 

...your page body 
1

更換

else 

else if (isset($_POST['username'])) { 

,如果你不希望告警,當用戶不嘗試登錄出現。

0
you miss <?php and Varible 
    this Code Use then Check 
     <?php 

      $username = $_POST['username']; 
      $password = $_POST['password']; 

      //This query is to check whether ther user has provided valid details 
      $query = mysql_query("Select * from accounts where username='".$username."' and  password='**".$password."**'"); 
      $status = mysql_num_rows($query); 

      /**Now checking whether the number of rows returned by database are greater than 0 to verify the login if number of rows are greater than zero we will redirect the user to index page with a session variable**/ 

      if($status >0) 
      { 
       $_SESSION['username'] = $username; 
      ?> 
      <meta http-equiv="refresh" content="0;url=index.php"> 
      **<?php** } 
      else 
      {?> 
      <script language="javascript"> 
      alert('UserName or Password does not match. Please try again'); 
      </script> 
      **<?php** }?> 
+0

這爲什麼解決這個問題? – phant0m