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

时间:2014-09-10 05:25:45

标签: php

登录 PHP 代码在我的本地服务器中正常运行。它也在实时服务器上运行,但总是在第一次尝试时失败而不会出现任何错误(验证或用户验证错误)。

无法将整个代码放在实时服务器上。在Web上发现了许多同样问题的问题(即使在stackOverflow上)但没有任何回答。

请帮忙!

登录页面:

<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代码:

  <?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);
}
?>
第1页的

代码//验证后

<?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");
?>

现在是page1的html代码,用于显示是否允许用户。

2 个答案:

答案 0 :(得分:0)

<?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");
}
?>

答案 1 :(得分:0)

尝试将其放在脚本的末尾:

<label for="user_location">Location</label>
<select id="user_location" name="user[location]">
  <option value="Earth">Earth</option>
  <option value="Mars">Mars</option>
  <option value="Jupiter">Jupiter</option>
</select>

如果您正在使用类(例如,使用框架控制器时),请将其置于析构函数中

session_write_close();
相关问题