Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login error message in php

Tags:

php

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.

login.php //login page

<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

<?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.

like image 855
user3112307 Avatar asked Dec 21 '13 08:12

user3112307


People also ask

How to display login error message in php?

The quickest way to display all php errors and warnings is to add these lines to your PHP code file: ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

How can I send error message from PHP to HTML?

By default, PHP sends an error log to the server's logging system or a file, depending on how the error_log configuration is set in the php. ini file. By using the error_log() function you can send error logs to a specified file or a remote destination.


2 Answers

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

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

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

if (isset($_SESSION['message']))
{
    echo $_SESSION['message'];
    unset($_SESSION['message']);
}
like image 104
MaGnetas Avatar answered Oct 23 '22 18:10

MaGnetas


Give a space between * and from in query.

$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"

in login.php

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

          ?>
like image 1
Jason W Avatar answered Oct 23 '22 17:10

Jason W