PHP登录表单验证失败

时间:2011-08-05 10:24:54

标签: php

我有以下登录表单指向文件'../ exe / form-exec.php'。

<form id="loginForm" name="loginForm" method="post" action="../exe/login-exec.php">
  <table width="300" border="0" align="center" cellpadding="2" cellspacing="0">
    <tr>
      <td width="112"><b>Login</b></td>
      <td width="188"><input name="login" type="text" class="textfield" id="login" /></td>
    </tr>
    <tr>
      <td><b>Password</b></td>
      <td><input name="password" type="password" class="textfield" id="password" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="Submit" value="Login" /></td>
    </tr>
  </table>
</form>

login-exec.php应该验证输入登录的位置&amp;密码已经是字段,如果没有返回到带有$errflag的表单文件。

文件login-exec.php:

//Start session
    session_start();

    //Include database connection details
    require_once('../inc/config.php');

    //Array to store validation errors
    $errmsg_arr = array();

    //Validation error flag
    $errflag = false;



..... 



//Input Validations
    if($login == '') {
        $errmsg_arr[] = 'Login ID missing';
        $errflag = true;
    }
    if($password == '') {
        $errmsg_arr[] = 'Password missing';
        $errflag = true;
    }

    //If there are input validations, redirect back to the login form
    if($errflag) {
        $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
        session_write_close();
        header("location: ../form/login-form.php");
            exit();
}

部分工作正常 - 它返回到表单 - 但没有显示错误。 任何建议都非常赞赏。

3 个答案:

答案 0 :(得分:1)

您正在对$_SESSION['ERRMSG_ARR']进行定值,但是当您返回表单时,您没有检查/使用它...尝试将其添加到表单代码

<?php 
session_start();
if(!is_null($_SESSION['ERRMSG_ARR'])) {
    echo '<div class="errmsg">Error...</div>';
}
?>
<form id="loginForm" name="loginForm" method="post" action="../exe/login-exec.php">
  <table width="300" border="0" align="center" cellpadding="2" cellspacing="0">
    <tr>
...

无论如何,这种类型的验证处理(使用会话参数)非常不鼓励 ......

请尝试关注一些design patterns;)

答案 1 :(得分:1)

在与html表单相同的页面上,执行与此类似的操作:

session_start();
foreach ($_SESSION['ERRMSG_ARR'] as $msg) {
  echo $msg . "<br />";
}
etc...

实际显示消息。

答案 2 :(得分:0)

使用POST方法从表单提交数据时,最好使用$_POST['login']而不是$login,因为如果register_globalsoff则赢了不行。 这可能是你的问题。

相关问题