2017-09-05 70 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

你有任何建议吗? –