2012-02-14 3 views
-7

우리는 php post가 통신을 데이터베이스로 리디렉션하는 양식이 있습니다. 로그인 실패로 인해 사용자 이름이 존재하지 않거나 비밀번호가 일치하지 않으면 쉽게 검색 할 수있는 방법이 필요합니다. 각 오류를 별도의 옵션으로 말하십시오.PHP 로그인 및 dbase 요청

<form id="form1" action="doLogin.php" method="post"> 
    <div id="border2"> 
    <table class="table_text" cellpadding="2" cellspacing="0" border="0"> 
     <tr> 
      <td>Username:</td> 
      <td><input type="text" name="username" class="input" /></td> 
     </tr> 
     <tr> 
      <td>Password:</td> 
      <td><input type="password" name="password" class="input" /></td> 
     </tr> 
     <tr> 
      <td colspan="2" align="right"><a target="_new" href="register.php">Register</a><input id="login_button" type="submit" name="submit" value="Login" class="button" /></td> 
     </tr> 
    </table> 
     </div> 
</form> 
+2

구문 분석 오류를보십시오 : 당신은 근처에 구문 오류가있는 "로그인 실패가 의심된다면 ... 더는 존재 username을하지 않으려면 ..." – JYelton

+3

을 당신이 피곤가 무엇? –

+0

로그인 스크립트에서 JSON 객체를 반환하여 오류 코드와 메시지를 자세히 설명합니다. 그런 다음 AJAX와 함께 사용할 수 있습니다. 또는이 양식과 동일한 페이지로 e = 1로 리디렉션 할 수 있습니다 (1은 오류 코드 임). – crush

답변

-1

function protect($string){ 
    $string = trim(strip_tags(addslashes($string))); 
    return $string; 
} 

if($_POST['submit']){ 
    $username = protect($_POST['username']); 
    $password = protect($_POST['password']); 
    if(!$username || !$password){ 
     //if not display an error message 
     echo "<script> alert('No data entered') </script>"; 
     echo "<script type='text/javascript'>document.location.href='index.php';</script>"; 

    }else{ 
     //if the were continue checking 
     //select all rows from the table where the username matches the one entered by the user 
     $res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."'"); 
     $num = mysql_num_rows($res); 
     //check if there was not a match 
     if($num == 0){ 
      //if not display an error message 
      echo "<script> alert('User does not exist') </script>"; 
      echo "<script type='text/javascript'>document.location.href='index_spa.php';</script>"; 

     }else{ 
      //if there was a match continue checking 
      //select all rows where the username and password match the ones submitted by the user 
      $res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."' AND `password` = '".$password."'"); 
      $num = mysql_num_rows($res); 
      //check if there was not a match 
      if($num == 0){ 
       //if not display error message 
       echo "<script> alert('Invalid Password') </script>"; 
       echo "<script type='text/javascript'>document.location.href='index.php';</script>"; 
      }else{ 
       //if there was continue checking 
       //split all fields fom the correct row into an associative array 
       $row = mysql_fetch_assoc($res); 
       //check to see if the user has not activated their account yet 
       if($row['active'] != 1){ 
        //if not display error message 
        echo "<script> alert('Your account " . $username . " is not activated yet.') </script>"; 
        echo "<script type='text/javascript'>document.location.href='index_spa.php';</script>"; 

       }else{ 
        //if they have log them in 
        //set the login session storing there id - we use this to see if they are logged in or not 
        $_SESSION['uid'] = $row['id']; 
        //show message 
        echo "<script type='text/javascript'>document.location.href='index.php?u=" . $username . "';</script>"; 
       } 
      } 
     } 
    } 
} 
+0

-1, 귀하의 코드는 교과서 [SQL 주입 취약점] (http://en.wikipedia.org/wiki/SQL_injection)과 [교차 사이트 스크립팅 취약점] (http://en.wikipedia.org/wiki)을 보여줍니다./Cross-site_scripting). 적절한 데이터베이스 매개 변수 이스케이프 또는 매개 변수화 된 쿼리를 사용하도록 코드를 수정하고 사용자에게 반환 할 때 사용자 입력을 안전하게 인코딩해야합니다. – Charles

+0

어떻게 말해 줄 수 있습니까? 필자는 입력 필드를 주입 ​​할 수있는 기회로부터 보호하기 위해 PHP 함수를 사용합니다. 그러나이 대답에서 나는 그것을 전혀 쓰지 않는다. 하지만이 -1? –

+0

나는 이미 이것을 편집했다. 승인? –