PHP脚本不适用于登录表单

时间:2012-05-25 07:11:26

标签: php login-script

我使用会话变量创建了一个登录表单。此登录表单是index.php,此表单操作在进一步操作之前将其重定向到validate.php页面。 Vaidate.php代码包含此

<?php
session_start();                    // Initialize session           
include('config.php');              // Include database connection settings
$sql= mysql_query("select * from users where (username= '". mysql_real_escape_string($_post['uname'])."') and (password='".mysql_real_escape_string($_post['pass'])."')");  // Retrieve username and password from database according to user's input
    // check if the username and password exists
    if(mysql_num_rows($sql)==1)
    {
        // store the USERNAME in SESSION VARIABLE
        $_SESSION['name'] = $_POST['uname']; 
        // and then JUMP to WELCOME page
        header('Location:welcome.php');
    }
    else
    {
        // Jump to login page
        //echo "<script type='javascript'>{alert('Username Or Password Incorrect')
//      return false;
//      }
        header('Location:index.php');
    }

?>

和index.php包含以下代码

<?php
session_start();                // function to start the session 

if (isset($_SESSION['name']))   // Check, if user is already login, then jump to Welcome page
    {
        header('Location: welcome.php');
    }
?>
</head>
<title>Login</title>

<body>
<form action="validate.php" method="post" name="log"/>
<h3 align="center" style="margin-top:40px">User Login</h3>
<table border="1" cellpadding="3" cellspacing="0" width="40%" align="center" style="margin-top:60px">
<tr>
    <td>User Name</td>
    <td >
        <input type="text" name="uname"/>
    </td>
</tr>
<tr>
    <td>Password</td>
    <td>
        <input type="password" name="pass"/>
    </td>
</tr>
<tr>
    <td colspan="2" align="center">
        <input type="submit" value="Submit">
        <input type="reset" value="Clear ">
    </td>
</tr>
</table>
</body>
</html>

这个问题是,即使用户名密码正确,它也会重定向到index.php。 在这种情况下,我无法弄清楚我错过了什么。代码似乎是正确的。

2 个答案:

答案 0 :(得分:1)

$ _ post ['pass']和$ _post ['uname']未定义。使用$_POST['pass']$_POST['uname']

答案 1 :(得分:1)

$ _ POST必须是资本。此外,我建议先将$ _POST ['uname']存储在变量中,然后在查询中使用它。