简体   繁体   English

PHP脚本不适用于登录表单

[英]PHP script not working for login form

I have made one login form using session variables. 我使用会话变量创建了一个登录表单。 This login form is index.php and this form action is redirecting it to a validate.php page before he proceeds further. 此登录表单是index.php,此表单操作在进一步操作之前将其重定向到validate.php页面。 Vaidate.php code contains this 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');
    }

?>

and index.php contains below code 和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>

This problem is that it is redirecting to index.php even if the username password is right. 这个问题是,即使用户名密码正确,它也会重定向到index.php。 I'm not able to figure out what I'm missing in this case. 在这种情况下,我无法弄清楚我错过了什么。 Code seems to be right. 代码似乎是正确的。

$_post['pass'] and $_post['uname'] are undefined. $ _post ['pass']和$ _post ['uname']未定义。 Use $_POST['pass'] and $_POST['uname'] . 使用$_POST['pass']$_POST['uname']

$_POST must be capital. $ _POST必须是资本。 Besides, i recommend to store the $_POST['uname'] in a variable first, then use it in the query. 此外,我建议先将$ _POST ['uname']存储在变量中,然后在查询中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM