2012-11-09 12 views
0

데이터가 데이터베이스의 세부 정보와 일치하지 않으면 사용자 입력과 일치하도록 데이터베이스에서 레코드를 가져 오는 사이트에서 작업하고 있습니다. 자바 스크립트 경고 상자를 표시해야합니다. 하지만 문제는 자바 스크립트 상자가 너무 시체를로드에 게재됩니다. 다음은 내 코드입니다.PHP에서 자바 스크립트 경고 사용하기

//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

자바! = 자바 스크립트 원하지 않는 경우. –

+5

** 경고 ** 귀하의 코드는 SQL 주입 공격에 매우 취약합니다. 또한 ** 사용자 ** 암호를 데이터베이스에 저장하지 마십시오. –

+1

@Jessica Brownie가 Daniel이 말한 것을 완성하기 위해, 암호 해시 (암호 만 해시하지 말고 항상 소금과 사용자 정의 부분과 같은 다른 부분을 포함하십시오)를 찾으십시오. –

답변

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

관련 문제