this is my login page coding. my login form html coding and php coding is in same page. now if login successful it will redirect to mainpage.php and then if login failure it will show the error message on same page (login.php). how to do that? i tried something but its not working... how to use php session here?

    $uname = $_POST['username'];
    $pwd = $_POST['password'];
    $uname = htmlspecialchars($uname);
    $pwd = htmlspecialchars($pwd);

    $user = "root";
    $pass = "";
    $database = "data";
    $link = mysql_connect( "localhost", $user, $pass );
        die ( "Could not connect to MySQL : " .mysql_error() );
    mysql_select_db ($database, $link) or die ( "Could not select MySQL $database : " .mysql_error() );
    $result = mysql_query( "SELECT * FROM emp_register WHERE emp_username = '$uname' AND emp_password = '$pwd'" );
    while($row = mysql_fetch_array($result))
    if($row["emp_username"] == $uname && $row["emp_password"] == $pwd)
    echo "Welcome $uname";
    echo "Username and Password does not match";

Edited by Indians

3 Years
Discussion Span
Last Post by minitauros

As both posters above are saying, you can use a header to redirect the user to another page. Example:

if($row["emp_username"] == $uname && $row["emp_password"] == $pwd)
    // Set the header that will redirect the user to the given page
    // when this page's PHP script is done loading.
    header('Location: mainpage.php');

    // Maybe we want to save some variables in a session. In that case,
    // example:
    $_SESSION['login'] = array(
        'user_id' => $uid, // Replace by the ID of the user you're logging in.
        'username' => $uname

    // (In order for the session to work, you need to have used session_start();
    // before).

    // There is no need to output this message, as the user is being
    // redirected anyway, but I'll lave it in tact for you ;).
    echo "Welcome $uname";
    echo "Username and Password do not match";

More info about session_start() on php.net: click.

Oh, and a thing to get you started on password encryption: click. Note that this tutorial is using the md5() function, of which the use is strongly unadvised (as it is seen as deprecated by almost everyone), but it will give you an idea of what password encryption is.

Edited by minitauros

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.