问题描述
我正在使用AJAX创建登录表单,我试图使其从4天开始生效,但无法做到这一点在这种情况下,我遇到的问题很少,即如果我输入有效的用户名&密码也然后它也没有登录该页面,我不知道我在哪里出错任何人都可以找出我出了什么问题的地方,这对帮助我们真的很有帮助.
I'm creating login form using AJAX, i,m trying to make it work from 4 days but unable to do so in this i have few issues i.e., if i enter Valid username & password also then also its not getting logged in to the page, i don know where im going wrong please can any one find it out where im going wrong, it would be really very help full.
login.php
login.php
<?php
session_start();
$mysqli = mysqli_connect("localhost","root","","ajax1");
?>
<!DOCTYPE HTML>
<html>
<head>
<title> login script with ajax</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body style="background-color:#b3ffff">
<div style="padding-left:500px ; padding-top:200px">
Username: <input id="username" type="text" name="username" placeholder="username"><br><br>
Password: <input id="password" type="password" name="password" placeholder="Password"><br><br>
<input id="submit" name="submit" type="button" value="Log In">
<p style="color:black">Havent Registered? <a href="index.php">Register</a>.</p><br><br>
<div id="display" style="color:red"></div>
<script>
$(document).ready(function(){
$("#submit").click(function(){
var password = $("#username").val();
var password = $("#password").val();
var datastring = 'username=' + username + '&password=' + password;
if(username=='' || password==''){
$("#display").html("Please Enter All The Fields");
}
else{
$.ajax({
type: "POST",
url: "success.php",
data: datastring,
cache: false,
success: function(result){
$("#display").html(result);
window.location = "welcome.php";
}
});
}
return false;
});
});
</script>
</div>
</body>
</html>
success.php
success.php
<?php
$mysqli = mysqli_connect("localhost","root","","ajax1");
session_start();
if (isset($_SESSION['id'])){
header('location:welcome.php');
}
$myusername = mysqli_real_escape_string($mysqli,$_POST['username']);
$mypassword = mysqli_real_escape_string($mysqli,$_POST['password']);
$sql = "SELECT * FROM users WHERE username = '$myusername' AND password='$hashed_password'";
$result = mysqli_query($mysqli,$sql);
$row = mysqli_fetch_array($result);
$hashed_password=$row['password'];
if(password_verify($mypassword, $hashed_password)) {
$_SESSION['login_user'] = $myusername;
//$_SESSION['id']=$row['userid'];
echo'Successfully Registered';
exit();
}
else
{
echo'Invalid username or password';
}
?>
welcome.php
welcome.php
<?php
session_start();
if (!isset($_SESSION['id'])) {
header('location:login.php');
}
?>
<!DOCTYPE html>
<html>
<body>
<div style="Padding-left:200px; padding-top:100px">
<?php
$mysqli = mysqli_connect("localhost","root","","ajax1");
$query=mysqli_query($mysqli,"select * from `users` where userid='".$_SESSION['id']."'");
$row=mysqli_fetch_array($query);
echo 'Welcome - '.$row['username'];
?>
<!--br><br>
<a href="logout.php">Logout</a>
<br><br-->
</div>
</body>
</html>
推荐答案
在您的login.php上的Submit函数中,您将变量设置为错误.将它们更改为此:
In your submit function on your login.php you have set the your variables wrong. Change them to this:
var username = $("#username").val();
var password = $("#password").val();
执行完此操作后,执行v Sugumar在他的回答中建议的内容.
After you do that, do what v Sugumar suggested in his answer.
这篇关于显示错误的密码或用户名,即使它是正确的密码或用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!