My PHP code for login works fine in my local server. It's also working on live server but always fails at first attempt without giving any error (validation or user verification errors).
Can't put whole code as it's on a live server. Found many questions on web with same problem (even on stackOverflow) but without any answwer.
Please help!
Login Page:
<form class="form-signin" id="form-signin" action="./" name="LoginForm" role="form">
<h2 class="form-signin-heading">Please Sign In</h2>
<div class="input-group">
<span class="input-group-addon"></span>
<input type="text" class="form-control" id="username" name="username" placeholder="Username" autofocus>
</div>
<div class="input-group">
<span class="input-group-addon"></span>
<input type="password" class="form-control" id="password" name="password" placeholder="Password" >
</div>
<button type="submit" class="btn btn-lg btn-primary btn-block" id="signinBtn" data-loading-text="Verifying...">Sign in</button>
</form>
javascript:
$(document).ready(function(){
$("#form-signin").submit(function(){ // actions when form is submitted
$("#msg").hide(); //hide the danger alert
var btn = $("#signinBtn");
btn.button('loading'); // change sign-in button state to loading till ajax complete
username = $("#username").val();
password = $("#password").val();
if ((username==null || username=="") || (password==null || password=="") ) // i.e if any of the username password field is empty
{
$("#msg").html("Please Fill the form completely");
btn.button('reset');
$("#form-signin").effect('shake'); // shake effect --> needs jquery.ui
$("#msg").show(200);
return false;
}
else{
$.ajax({
type: "POST",
url: "verify.php",
data: "username="+username+"&password="+password,
success: function(verified){
// if user is verified redirect according to the level of user
if(verified == 'true1')
window.location = "http://example.com/page1.php";
else if(verified == 'true2')
window.location = "http://example.com/page2.php";
else if(verified == 'true3')
window.location = "http://example.com/page3.php";
else if(verified == 'false')
{
//else display error
$("#msg").html("<strong>Invalid</strong> username password combo!");
$("#form-signin").effect('shake');
$("#msg").show(200);
btn.button('reset');
}
}
});
return false;
}
}); // .submit function ends
}); //.ready function ends
PHP code:
<?php
include("DataBaseLogin.php");
session_start();
$user = $_POST["username"];
$pass = $_POST["password"];
$user = htmlspecialchars($user);
$pass = htmlspecialchars($pass);
$user = mysql_real_escape_string($user, $db_handle);
$pass = mysql_real_escape_string($pass, $db_handle);
if ($db_found)
{
$result = mysql_query("SELECT * FROM user_table WHERE uname = '$user' AND pword = '$pass'");
$resultArray=mysql_fetch_assoc($result);
if($resultArray['uname']==$user && $resultArray['pword']==$pass)
{
//If user verified
$_SESSION['login'] = "1";
$_SESSION["current_user"]=$user;
$_SESSION['Level'] = $resultArray['Level'];
$Level = $_SESSION['Level'];
if($Level==1)
echo 'true1';
else
if($Level==2)
echo 'true2';
else if($Level==3)
echo 'true3';
}
else
{
$_SESSION['login'] = "";
echo 'false';
mysql_close($db_handle);
}
}
else
{
echo "db not found";
mysql_close($db_handle);
}
?>
code for page1 //after verification
<?PHP
session_start();
include("DataBaseLogin.php");
if (!(isset($_SESSION['login']) && $_SESSION['login'] != ''))
{
header ("Location: index.php");
}
else if($_SESSION['Level'] !=1)
die("You are not allowed on this page");
?>
now html code for page1 to show if user is allowed.