2012-10-10 5 views
2

로그인 시스템에 script/ajax를 사용하고 싶습니다.javascript/ajax login system

<script type="text/javascript"> 
$(document).ready(function() { 
$("input[type=button]").click(function() { 
var username = $('input[id=username]').val(); // get the username 
var password = $('input[id=password]').val(); // and the password 
    if (username != '' || password !='') { // if not empty 

    $.ajax({ 
      type: "POST", 
      url: "loginUser.php", 
      data : "username="+username+"&password="+password, 
      dataType: "json", 
      success: function (data) { 
      var success = data['success']; 
      if(success == 'false'){ 
      var error = data['message']; 
      alert(error); // the array with all the errors 
      }else{ 
     $('#mask , .login-popup').fadeOut(300 , function() { 
     $('#mask').remove(); 
    });// end fadeOut function() 
setTimeout("location.href = 'home.php';",1500); 
    } 
    } 
    });//end success function 

    } else { 
alert('Enter some text ! '); // just in case somebody to click witout writing anything :) 
    } 
    });//end click function 
});//end ready function 
    </script> 

loginUser.php는 기본적으로 모든 기능을 확인 : 지금까지 나는에 로그가 성공적으로 세트가 $ _SESSION [ 'ID'] 인 경우, 모든 오류와 배열을 다시 얻을 수있었습니다 이 같은 데이터를 다시 보내

if (empty($_POST)===false){ 
$email = sanitize($_POST['username']); 
$password = sanitize($_POST['password']); 


if (empty($email) === true || empty ($password) === true){ 

    $errors[] = 'You need to enter a username and password'; 
} else if (mail_exists($email) === false){ 
    $errors[] = 'we can\'t find that username. have you registered?'; 
}else if (user_active($email) === false){ 
    $errors[] = 'you haven\'t activated your account!'; 
}else { 
$login = login($email, $password); 
if ($login === false){ 
    $errors[] = 'username/password combination is incorrect!'; 
}else { 
//set user session 
$_SESSION['id'] = $login; 

//redirect user to home 
    echo json_encode(array("success"=>'true' 
       )); 
} 

    } 
echo json_encode(array("success"=>'false', 
      "message"=>$errors,    
      )); 
          } 

내가 말했듯이, 내가 암호와 사용자 이름이 올바르지 않은 경우 모든 경고를 얻을 및 암호와 사용자 이름이 올바른지하지만 팝업이 표시 상태를 유지하면 나는 $ _SESSION 세트를 얻을 I 리다이렉트하지 마라. (하지만 세션 세트 때문에 액세스 할 수있다.) 성공이 == true 또는 == false인지 테스트하는 것이 맞습니까 ???

* * * 수정 : 문제가 PHP 파일에 있습니다. 내 대답 좀 봐 ....

+0

노력하고, 내가 정말 이해할 수없는 문제는 PHP 파일의 경우 다른 논리에 있었다 밖으로 ... 어떤 생각? – mat

+0

콜백시 리디렉션. 로그인 후 사용자를 다른 페이지로 리디렉션하는 경우 리다이렉션시 로그인을 수행 할 수 있습니다. 아약스 필요 없음 – aurbano

+0

@mattia : 두 경우 (유효 로그인과 무효 로그인)의 "데이터"는 무엇입니까? – Irfan

답변

2

고정!

JS :

<script type="text/javascript"> 
$(document).ready(function() { 
     $("input[type=button]").click(function() { 
     var username = $('input[id=username]').val(); // get the content of what user typed (in textarea) 
     var password = $('input[id=password]').val(); // get the content of what user typed (in textarea) 
          $.ajax({ 
          type: "POST", 
          url: "loginUser.php", 
          data : "username="+username+"&password="+password, 
          dataType: "json", 
          success: function (data) { 
          var success = data['success']; 
          if(success == false){ 
     var error = data['message']; 
          alert(error); // just in case somebody to click on share witout writing anything : 


           } 

           if(success == true) { 
    $('#mask , .login-popup').fadeOut(300 , function() { 
    $('#mask').remove(); 
           });// end fadeOut function() 
    setTimeout("location.href = 'home.php';",1000);         
               } 
                } 

         });//end ajax    
       });//end click function 
     });//end ready function 

loginUser.php :

여전히
<?php 
    if (empty($_POST)===false){ 
$email = sanitize($_POST['username']); 
$password = sanitize($_POST['password']); 

if (empty($email) === true || empty ($password) === true){ 
      $result = false; 
      $errors = 'You need to enter a username and password'; 
      echo json_encode(array("success"=>$result, 
            "message"=>$errors,    
            )); 
} else if (mail_exists($email) === false){ 
      $result = false; 
      $errors= 'we can\'t find that username. have you registered?'; 
      echo json_encode(array("success"=>$result, 
            "message"=>$errors,    
          )); 
}else if (user_active($email) === false){ 
      $result = false; 
      $errors = 'you haven\'t activated your account!'; 
      echo json_encode(array("success"=>$result, 
          "message"=>$errors,    
          )); 
}else { 
$login = login($email, $password); 
if ($login === false){ 
      $result = false; 
      $errors = 'username/password combination is incorrect!'; 
      echo json_encode(array("success"=>$result, 
          "message"=>$errors,    
          )); 
} else { 
//set user session 
$_SESSION['user_id'] = $login; 
$result = true; 
    echo json_encode(array("success"=>$result 
           )); 

} 
} 
} 
?>