用户登录无效


user invalid login

我正在创建自己的网站只是为了获得一些经验。我已经用了3天的时间,现在我可以注册和登录了。

登录时,如果在数据库中没有找到用户名和密码的组合,我的代码显示一条错误消息,告诉用户他还没有注册,或者他输入了错误的用户电子邮件或密码。

但是,消息显示在新页面中,而不是在登录页面中。

我在网上看了一些教程,但是没有找到一个很好的解释。有人能给我一些建议吗?

我使用PHP数据库连接。

我刚刚输入了一个非常基本的例子:

<?php
//login.php
$msg = '';  //to store error messages
//check whether the user is submitting a form
if($_SERVER['REQUEST_METHOD'] == 'POST')    //check if form being submitted via HTTP POST
{
    //validate the POST variables submitted (ie. username and password)
    //check the database for a match
    if($matchfound == TRUE) //if found
    {
        //assign session variables and other user datas
        //then redirect to the home page, since the user had successfully logged in
        header('Location: index.php');
    }
    else
    {
        $msg = 'Error. No match found !';   //assign an error message
        include('login_html.php');  //include the html code(ie. to display the login form and other html tags)
    }
}
else    //if user has not submitted the form, just display the html form
{
    include('login_html.php');
}
//END of login.php
?>

login_html.php:

<html>
  <body>
<?php if(!empty($msg)) echo $msg; ?> <!-- Display error message if any -->
<form action="login.php" method="post">
  <input name = "username" type="text" />
  <input name = "password" type="password" />
  <input name = "submit" type="submit" value="Submit" />
</form>
</body>
</html>

这不是一个完整的代码。但我只是为了让你们明白这是怎么做到的。:)

好运

您的开始表单标记应该看起来像这样:<form action="" method="post">。空的"action"属性将导致页面发送回其自身。只需检查$_POST的用户名和密码,以确定是否测试匹配或只显示表单。

请确保您的密码散列并对您的输入进行消毒!

你可以做到这一点,而无需切换到新页面。

   <?php session_start(); ?>
   <?php
   if(isset($_POST) && isset ($_POST["admin_login"])){
        $user_data_row = null;
        $sql="SELECT * FROM table_name WHERE <table_name.field name>='".mysql_real_escape_string($_POST['email'])."'
            and <table_name.field name='".mysql_real_escape_string($_POST['password'])."'
           ;
        $result=mysql_query($sql);
        $user_data_row=mysql_fetch_assoc($result);
        if(is_array($user_data_row)){
            $_SESSION['user_id'] = $user_data_row['id'];
            header("Location: <your page name>");
        }else{
            $_SESSION['message'] = "Valid email and password required";
        }
    }
 ?>

<?php if(isset($_SESSION['message'])){
     echo "<li>{$message}</li>";
 ?>
 <form  action="" method="post"  id="customForm">
               <label>Email:</label>
               <input type="text" id="email" name="email">
               <label>Password:</label>
               <input type="password" id="password" name="password">
                <input type="submit" value="Login" id="send" name="admin_login">
        </form>
    may be its helps you....

基本上,你需要做的是将表单发布到同一页面。

一旦你有了,在类型检查$_POST: if($_SERVER['REQUEST_METHOD'] == 'POST')

如果是帖子,检查用户名和密码并显示错误或重定向到已登录的页面。之后,显示登录表单。

如果是错误,他们会得到错误然后是登录表单。如果没有发布,他们只会得到登录表单,如果是有效的登录,他们会在显示登录表单之前被重定向到正确的页面。