SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    May 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    to display error message if login fails on login form in php

    I have written code for login and it redirects to deifferent pages according to user role but problem is that if there is wrong username or password it does not show error it simply shows blank page Please help me to show error meassge for wrong password or username.

    loginform.php(html code)
    <div id="login_form">

    <form method="post" action="login.php">
    <label>User Name/label>
    <input type="text" id="username" name="username" />
    <label>Password/label>
    <input type="password" id="password" name="password" />
    <label></label><br/>
    <input type="submit" id="login" value="Login" />
    <input type="button" id="cancel_hide" value="Cancel" />

    </form>
    </div>
    <div id="shadow" class="popup"></div>

    jquery:
    $(document).ready(function(e) {
    $("#shadow").fadeIn("normal");
    $("#login_form").fadeIn("normal");
    $("#user_name").focus();

    $("#cancel_hide").click(function(){
    $("#username").val()="";
    $("#password").val()="";
    });
    $("#username").change(function(e) {

    });
    $("#login").click(function(){

    if($("#username").val().length==0 && $("#password").val().length==0)
    {
    // $(this).next().html("Field needs filling");
    $("#username").after('<span class="errorkeyup">Field cannot be empty</span>');
    $("#password").after('<span class="errorkeyup">Field cannot be empty</span>');
    //return false;
    success = false;
    }

    });
    });

    login.php

    <?php
    session_start();
    include('connection.php');

    $username = $_REQUEST['username'];
    $password = $_REQUEST['password'];

    $username = mysql_real_escape_string($username);
    $password = mysql_real_escape_string($password);

    $login = mysql_query("select * from userlogin where username = '$username' and password = '$password'");
    $num_row = mysql_num_rows($login);
    $row=mysql_fetch_array($login);
    if($num_row == 1) {
    echo "Login Successfull";
    $_SESSION['username']=$row['username'];
    $_SESSION['userrole']=$row['usertype'];
    if($row['usertype']=='admin')
    {
    header("Location:mainpage.php");
    //header("Location:u2.php");
    exit();
    }
    else if($row['usertype']=='user')
    {
    header("Locationrofilepg.php");
    exit();
    }
    }
    else {


    // exit();
    /*echo '<script type="text/javascript">
    document.getElementById("add_err").innerHTML= "Error";
    window.history.back();
    </script>';*/
    //header("Location:loginform.php");
    }

    ?>

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,387
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    In your login.php code, you need to store the error message in a session variable - you could do something like this:
    PHP Code:
    $_SESSION['errors'] = array("Your username or password was incorrect.");
    header("Location:loginform.php"); 
    Then, in your loginform.php file, add something like this above the form (or wherever you want the error to show):
    PHP Code:
    <?php if (isset($_SESSION['errors'])): ?>
        <div class="form-errors">
            <?php foreach($_SESSION['errors'] as $error): ?>
                <p><?php echo $error ?></p>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
    This will check if there are any errors set in $_SESSION and loop through and display them.

  3. #3
    SitePoint Member
    Join Date
    May 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for reply I tried your code now it does not display blank page and shows login form but it does not show error message

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,387
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Are you calling session_start() at the top of loginform.php?

  5. #5
    SitePoint Member
    Join Date
    May 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry I didn't called session_start() in loginform so code was not working for me but after adding in loginform error message is visible as soon as page is loaded it So how to solve this?


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •