2017-09-05 118 views
-3

我正在使用框架7,我試圖使用PHP(PDO)和Ajax登錄,但每次它出現「登錄失敗」甚至如果在電子郵件和密碼輸入中插入了正確的數據,也不會出現錯誤,我可以使用PHP(PDO)進行註冊,並且一切都很順利,但無法登錄,請告知有什麼問題我的代碼我無法登錄使用PHP(pdo)&AJAX

index.php 

<div class="pages navbar-through" class="login-screen"> 
<div data-page="index" class="page no-navbar no-toolbar"> 
<div class="page-content" style="background:url(img/bg1.png) ;background- 
size: 100%"> 
<div class="content-block-title center"> 
</div> 


<span style="float: right;color: white;margin:20px;font-size: 20px"> 
<b>عربي</b></span> 


<div class="list-block" style="margin:100px 70px auto 70px;"> 

<img src="img/logo2.png" style="height: 55px ;width: 100%;margin-bottom: 
60px;text-align: center;"> 
<div class="item-input" style="border-bottom: 1px solid gray"> 
<input type="text" placeholder="Email" id="email" name="email" style="font- 
size: 15px;color: white;" required> 
</div> 

<div class="item-input" style="border-bottom: 1px solid gray;margin-top: 
10px"> 
<input type="password" name="password" placeholder="password" id="password" 
style="font-size: 15px;color: white"; required> 
</div> 



<div style="text-align: center ;margin-top: 30px"> 
<a href="#" style="color: #00bcd4 ;font-size: 15px">Forgot your password ? 
</a> 
<span id="login_message"><img src="img/ajax-loader.gif" style="display: 
none;"></span> 

<center><button style="margin-top: 20px; background-color: #00bcd4;border- 
radius: 0px;height: 45px; width: 240px; color: white; text-align: center;" 
name="login-btn" class="list-button" id="login-btn" name="login-btn">Login</button></center> 



<p style="color: white;font-size: 13px;margin-top: 40px"> You don't have 
account? </p> 
<a href="registeration.php" style="color: #ffeb3b ;font-size: 
13px">REGISTER</a> 
</div> 
</div> 
</div> 
</div> 
</div> 
</div> 
</div> 
</div> 

my-App.js 

$(document).ready(function() { 
$('#login-btn').click(function() { 
var email = $('#email').val(); 
var password = $('#password').val(); 

if($.trim(email).length > 0 && $.trim(password).length > 0) 
{ 
$.ajax({ 
url:"login.php", 
method: "POST", 
data: {email:email, password:password}, 
cache: false, 
beforeSend:function() 
{ 
$('#login-btn').val("connecting..."); 
}, 
success:function(data){ 
if(data){ 
myApp.alert('Login successful'); 
} 
else{ 
myApp.alert('Login failed'); 
} 
} 
}); 
} 
}); 
}); 

login.php 

<?php 
require_once 'config.php'; 
session_start(); 
if(isset($_POST['login-btn'])){ 
$email = $_POST['email']; 
$password = $_POST['password']; 

$select = $db_con->prepare("SELECT * FROM user WHERE email='$email' and 
password='$password'"); 
$select->setFetchMode(PDO::FETCH_ASSOC); 
$select->execute(); 
$data=$select->fetch(); 
if($data['email']!=$email and $data['password']!=$pass) 
{ 
echo "invalid email or password"; 
} 
elseif($data['email']==$email and $data['pass']==$pass) 
{ 
$_SESSION['email']=$data['email']; 
echo "successful"; 
} 
} 
?> 
+2

你'prepare'然後查詢失敗'bindParam'就可以了,讓'prepare'毫無意義。您的代碼易受SQL注入攻擊。 – IsThisJavascript

+1

if ifset isset($ _ POST ['login-btn'])){' - 你沒有發送一個名爲''''的參數,如果你想要求助,至少做一個最小的努力並且適當地調整你的代碼 –

+1

'登錄btn'與您的AJAX請求,所以這將永遠是錯誤的... – CBroe

回答

2

login.php文件有此檢查嘗試登錄前:

if(isset($_POST['login-btn'])){ 

豪如果你改變你的if語句來檢查$_POST['email']代替

data: {email:email, password:password}, 

:版本,你的AJAX腳本不發送一個名爲「登錄-BTN」的值,只有所謂的「電子郵件」值和值稱爲「密碼」 ,它應該工作,假設沒有其他問題。

請注意,您的查詢容易受到SQL注入的影響。請使用綁定的參數,而不是直接在查詢中使用的變量:

$select = $db_con->prepare("SELECT * FROM user WHERE email=:email and password=:password"); 
$select->setFetchMode(PDO::FETCH_ASSOC); 
$select->execute(array(':email' => $email, ':password' => $password); 
+0

我已經改變if(isset($ _ POST ['login- btn'])){to $ _POST ['email'],現在即使證書不正確,它也總是能夠成功登錄。 –

+0

這是因爲你的JavaScript成功處理程序只檢查'if(data){',因爲'data'是一個字符串(字符串echo'd來自PHP),它總是會是'真的' – rickdenhaan

+0

你有任何建議嗎? –