Tuesday, July 2, 2013

PHP Login 002

This program verifies user access and retrieves its corresponding data.

[config.php]
<?php
    $db_user = "root"// mysql username to access the database with.
    $db_pass = "crawl"// mysql password to access the database with.
    $database = "crawler_attendance"// the name of the database.
    $server = "localhost"// server to connect to.
    $table = "employees";    // the table that this script will set up and use.
    $link = mysql_connect($server, $db_user, $db_pass);
    mysql_select_db($database,$link);
?>



[index.html]

 <html>
    <head>
        <title>Php Simple Login Form</title>
        <link rel="stylesheet" type="text/css" href="style.css" />
    </head>
    <body>
        <div id="containt" align="center">
            <form action="login.php" method="post">
                <div id="header"><h2 class="sansserif">Admin/Employee Login</h2> </div>
                 <table>
                    <tr>
                        <td>Enter ID:</td>
                        <td><input type="text" name="id_num" size="10"></td>
                    </tr>
                         
                    <tr>
                        <td>Enter Password:</td>
                        <td><input type="password" name="password" size="10"></td>
                    </tr>
                    <tr>
                         <td><input type="submit" value="Log In"></td>
                    </tr>
                 </table>
            </form>
        </div>
    </body>
</html>



[login.php]
 <?php
    session_start();

    include("config.php"); //including config.php in our file
    $id_num = $_POST["id_num"]; //Storing username in $username variable.
    $password = $_POST["password"]; //Storing password in $password variable.

    $query = "select id, firstname, lastname from $table where company_id_no='".$_POST['id_num']."' and password = '".$_POST['password']."';";

    $execqry = mysql_query($query);
    $getValue = mysql_fetch_array($execqry); //place query result in an array?

    $id_firstname= $getValue['firstname'];
    $num_rows = mysql_num_rows($execqry);

    if ($num_rows <= 0) {
        echo "Sorry, incorrect credentials entered. <BR>";
        echo "Try again";
        exit;
    } else {
        $_SESSION['user']= $id_firstname;
        header("location:display.php");
        // It is the page where you want to redirect user after successful login.
    }
?>


[display.php]

<?php
session_start();
echo "Hello ". $_SESSION['user']. ".";
?>

No comments:

Post a Comment