dtcmadj31951 2014-09-10 05:25
浏览 14

首次尝试时登录尝试失败,但之后仍然有效

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.

  • 写回答

2条回答 默认 最新

  • dongxian6715 2014-09-10 06:48
    关注
    <?PHP
    session_start();
    include("DataBaseLogin.php");
    if (isset($_SESSION['login']) && !empty($_SESSION['login']))
    {
     header ("Location: page1.php");
    }
    else if($_SESSION['Level'] !=1 || empty($_SESSION['login'])) {
      header ("Location: index.php");
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大