【问题标题】:Echo form and reload on same page after submit提交后回显表单并在同一页面上重新加载
【发布时间】:2015-12-07 01:45:45
【问题描述】:

我有一个由 echo 显示的表单,我正在尝试在提交后刷新页面,以便在输入无效详细信息时可以添加登录尝试以计数当前显示表单但页面未再次刷新以添加数一数,这就是我所拥有的:

  $pdo = new PDO($dsn, $username, $password, $options);

  $max_time_in_seconds = 10;

  $max_attempts = 3;




if(login_attempt_count($max_time_in_seconds, $pdo) <= $max_attempts) {

    $form = "<form name='login' class='profile' id='form_id' action=''   
    method='post' accept-charset='utf-8'>
    <fieldset>
            <legend>Login</legend>
            <label for='username'>User name</label>
            <input type='text' name='username' id='username' autofocus  
            placeholder='User name MAX 10'  maxlength='10' title='You can 
            use characters & numbers' required />
            <label for='password'>Password</label>
            <input type='password' id='password' name='password'  
             />
             <br>
            <input type='submit' class='btn' value='Login' id='submit'   
             onSubmit='window.parent.location.reload();' />

           </form>";  
} else {

   $msg2 = " You have been BLOCKED try again in 10 minutes";



}

如果我刷新页面 3 次我被阻止,这就是我想要的如果提交输入 3 次 我已经尝试添加

       <?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>

我只是得到空白屏幕......

         if($_SESSION['user'] { header("location: memberlist.php");  }
         else
         if($_SESSION['user']['blacklisted_users'] == 'blocked') {       
         header("location: blocked.php");}
         else



        header("Location: private.php");
        die("Redirecting to: private.php");
         }

        else
         {

        $msg = " Username or Password INCORRECT";

          }

我认为最后的 else 正在阻止页面刷新,因为它只是在回显错误

登录检查:

      $query = "
        SELECT
            id,
            username,
            password,
            salt,
            email,
            firstname,
            surname,
            address,
            town,
            postcode,
            type,
            blacklisted_users
        FROM users
        WHERE
            username = :username
    ";


    $query_params = array(
        ':username' => $_POST['username']);


    try
    {
        $stmt = $db->prepare($query);
        $result = $stmt->execute($query_params);

    }
    catch(PDOException $ex)
    {

        die("Failed to run query: " . $ex->getMessage());
    }


    $login_ok = false;


    $row = $stmt->fetch();
    if($row)
    {

然后是密码检查......

【问题讨论】:

    标签: javascript php forms


    【解决方案1】:

    您忘记关闭&lt;fieldset&gt; 标签。在结束表单标记 &lt;/form&gt; 之前添加 &lt;/fieldset&gt;

    编辑:
    您的
    中有一些错误 您应该在 header(); 之后添加 exit();
    另外,$_SESSION['user'] 只是一个变量,将其更改为 isset($_SESSION['user']) 因为我认为这就是你想要做的

    你也错过了一些{}

    if(isset($_SESSION['user']) { header("location: memberlist.php");  }
             else { // You missed another one here I think?
             if($_SESSION['user']['blacklisted_users'] == 'blocked') {       
             header("location: blocked.php");
             exit();    
         } else { // You missed one here
    
    
    
            header("Location: private.php");
            exit(); // Change die to exit
             }
    
            else
             {
    
            $msg = " Username or Password INCORRECT";
    
              }
    

    【讨论】:

    • 我已经关闭了标签,如果我将表单操作留空,那么它会加载但在提交时不会刷新以添加到计数。
    • 所以您的页面会重新加载,但它不会在 login_attempts 中添加一个?如果是这样,您能否发布函数 login_attempt_count 的代码
    • 我不认为功能是问题我想我知道是什么导致了问题,让我编辑问题。
    • 现在几乎实际上页面正在刷新并计数添加和用户在 3 次尝试后被阻止,但现在 $msg 没有显示在错误的凭据上
    • 我再次编辑了我的代码,也许这行得通(我又删除了一个可能的错误)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 2015-01-17
    • 2011-11-08
    • 2013-01-01
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    相关资源
    最近更新 更多