php php中的登录错误信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20717032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:27:40  来源:igfitidea点击:

Login error message in php

php

提问by user3112307

I am new to PHP code and here I want to display an error message in login.php if the user entered incorrect userid or password.below I have written two-page code. login.php page submits the username and password to the check.php page. if username and password are correct then it redirects to the xyz.php else to the login page.

我是 PHP 代码的新手,如果用户输入了错误的用户 ID 或密码,我想在 login.php 中显示错误消息。下面我编写了两页代码。login.php 页面将用户名和密码提交到 check.php 页面。如果用户名和密码正确,则重定向到 xyz.php 否则重定向到登录页面。

login.php//login page

login.php//登录页面

<form name="login" enctype="multipart/form-data" action="checkpage.php" method="post">
            <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <th>Username</th>
                <td><input type="text" name="username" value="sandeep" onfocus="this.value=''" class="login-inp" /></td>
            </tr>
            <tr>
                <th>Password</th>
                <td><input type="password" name="password" class="login-inp" /></td>
            </tr>
            <tr>
                <th></th>
                <td valign="top"><input type="checkbox" class="checkbox-size" id="login-check" /><label for="login-check">Remember me</label></td>
            </tr>
            <tr>
                <th></th>
                <td><input type="submit" class="submit-login"  /></td>
            </tr>

            <tr><th></th><td>w want to display error message here..</td></tr>

</table></form>

checkpage.php//connection page

checkpage.php//连接页面

<?php
    session_start();
    //connecting to db
    $username=$_POST['username'];
    $pwd=$_POST['password'];
    $q="select *from xyz where username='$username' AND password='$pwd'";
    $qry=mysql_query($q);
    if(mysql_num_rows($qry)>0)
    {
        $_SESSION['username']=$username;
        echo "<script>window.open('xyz.php','_self')</script>"; 
        }
        else{

            header("location:login.php");

            }
?>

the above code work fine but i want to display the error message how could i display the error message. please guide me.

上面的代码工作正常,但我想显示错误消息我怎么能显示错误消息。请指导我。

回答by MaGnetas

You can set the message to session in your check.php script and then unset immediately after getting it for display (AKA "read once"):

您可以在 check.php 脚本中将消息设置为 session ,然后在获取显示后立即取消设置(又名“读取一次”):

$_SESSION['message'] = 'Your message';

and then (in login.php, where you want the message to be displayed):

然后(在 login.php 中,您希望在其中显示消息):

if (isset($_SESSION['message']))
{
    echo $_SESSION['message'];
    unset($_SESSION['message']);
}

回答by Jason W

Give a space between * and from in query.

在查询中的 * 和 from 之间留一个空格。

$q="select * from xyz where username='$username' AND password='$pwd'";
  1. For error message you can set a flag like error= 1

    header("location:login.php?error=1"); and in login.php you can check this flag and display a message like

      <?php   If(isset($_GET['error']) && $_GET['error'] == 1){ ?>
            <h3>Invalid username or password</h3>    
      <?php } ?>
    
  2. You can set a session of error and display it in login.php and unset it after displaying.

    but donot forget to use session_start in both pages session_start(); //on top of page

    before header code add $_SESSION['error'] = "you message here"

  1. 对于错误消息,您可以设置一个标志,如 error= 1

    header("location:login.php?error=1"); 在 login.php 中,您可以检查此标志并显示如下消息

      <?php   If(isset($_GET['error']) && $_GET['error'] == 1){ ?>
            <h3>Invalid username or password</h3>    
      <?php } ?>
    
  2. 您可以设置一个错误会话并在login.php中显示并在显示后取消设置。

    但不要忘记在两个页面 session_start() 中都使用 session_start;//在页面顶部

    在头代码添加之前 $_SESSION['error'] = "you message here"

in login.php

在登录.php

          <?php if(isset($_SESSION['error'])) {
                echo $_SESSION['error'];
           }

          ?>

回答by santoshkakkerla

else {
    echo 'invalid username or password';
    header("location:login.php");
}

回答by David Mark

    if ($result->num_rows > 0)
    {       
        $result = $result->fetch_assoc();

        $_SESSION['id_admin'] =     $result['id_admin'];
        $_SESSION['email'] =        $result['email'];
        $_SESSION['name'] =         $result['name'];
        $_SESSION['surname'] =      $result['surname'];
        $_SESSION['pass'] =         $result['pass'];


        header('Location: http://localhost/admin/dashboard.php');
        exit();
    }
    else 
    {
        echo  '<div class="alert alert-danger">
                <a href="#" class="close" data-dismiss="alert" aria-label="close">Close X</a>
                <p><strong>Alerta!</strong></p>
                Email or password wrong! Please try again!.
            </div>';
    }

    $conn->close(); 
}

?>

回答by Wassim Iro

Use Echo and include it at last thing on your page !!

使用 Echo 并将其包含在页面上的最后一件事中!!

<?php
//put it in the end of your php code
else {
    echo 'your message ';
}
?>