在PHP中登录错误消息

时间:2013-12-21 08:21:02

标签: php

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

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 //连接页

<?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");

            }
?>

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

7 个答案:

答案 0 :(得分:8)

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

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

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

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

答案 1 :(得分:1)

在查询中提供*和from之间的空格。

$q="select * from xyz where username='$username' AND password='$pwd'";
  1. 对于错误消息,您可以设置error= 1

    之类的标记

    标题( “?位置:login.php中错误= 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"

  3. 之前

    在login.php中

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

    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(); 
}

?>

答案 4 :(得分:0)

使用Echo并将其包含在页面上的最后一句话!!


declare interface BigInt {
  // here all magic you want to declare
  test: number
};

declare var BigInt: BigIntConstructor;

declare interface BigIntConstructor {
    new(value?: any): BigInt;
    (value?: any): BigInt;
    readonly prototype: BigInt;
};



function convert(inp: string): BigInt {
    return BigInt(inp);
}

const val = BigInt(10).test; // an example.

答案 5 :(得分:0)

这是如何在代码中显示错误消息的代码。

    <?php
if(isset($_POST['Username'])){
//session_start();
 $username = $_POST['Username'];
 $password =  $_POST['Password'];
//$r = mysqli_num_rows(mysqli_query($conn,"select * from  user where u_nm='".$username."' and password='".$password."'"));
$rs = mysqli_query($conn,"select * from user where Username='".$username."' and Password='".$password."'");
$cnt = mysqli_num_rows($rs);
//echo $username;
//echo $password;
//echo $cnt;
if($cnt >= 1)
{
    
    $_SESSION['username'] = $username;  
    header("Location:lessons.php");
}
else
{
    $msg = "<center><h4>Username or Password are not correct, try again.</center></h4>";
    header("Location:login.php?msg=$msg");
}
}
?>

答案 6 :(得分:-1)

<?php

 header("Access-Control-Allow-Origin: http://localhost/cta-rest-api/");
 header("Content-Type: application/json; charset=UTF-8");
 header("Access-Control-Allow-Methods: POST");
 header("Access-Control-Max-Age: 3600");
 header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, 
 Authorization, X-Requested-With");


 include_once 'Database.php';

  $email= isset($_POST['email']) ? $_POST['email'] : '';
  $password= isset($_POST['password']) ? $_POST['password']: '';

  $k="jazz";
  $salt=md5($k);
  //$password = password_hash(isset($_POST['password']), PASSWORD_BCRYPT).$salt;




       $add = "SELECT * from cta_user WHERE email='.$email.' and 
 password='".$password."'";

 $result = mysqli_query($conn,$add);

  if($result->num_rows>0)
 {
  $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
 // $active = $row['active'];

  $count = mysqli_num_rows($result);

  // If result matched $myusername and $mypassword, table row must be 1 row

  if($count == 1) {
     echo "success";


   //  header("location: welcome.php");



  }else {
     $error = "Your Login Name or Password is invalid";
      echo "error";

  }     


 /*if ($conn->query($add) === TRUE) {
    echo "successfull";


  } else {
    echo "error";
  }*/
 ?>