Thursday, November 30, 2017

Login Form

How to Create Login Form


Login Form
1. Create MySQL Users / Users Table
To create a database and table for example database name is "cms" and table name is "user",then run this code in your MySQL software, you can use phpMyAdmin.
    fieldname: user_id
    type: int (4) auto increment set as primary key

    fieldname: username
    type: varchar (20)

    fieldname: password
    type: varchar (50)

    if successful then your table will be like in the picture below.
Then insert to user table with data like below (make sure when enter password select dropdown function select MD5)


    username: dodo
    password: passwordnyadod0

    username: tri
    password: password1

    Now display your data in user table like picture below.
2. Create Script Code Login Form
For php files we need some files :
    1. index.php
    2. login.php
    3. logout.php
    4. database.php
Index.php will act as the dashboard page, while the database.php page is the page that contains the connect function to the database we created earlier
index.php
<? php
        session_start ();
        $ username = $ _SESSION ['username'];
        $ isLoggedIn = $ _SESSION ['isLoggedIn'];
        
        if ($ isLoggedIn! = '1') {
            session_destroy ();
            header ('Location: login.php');
        }
        ?>
        
        Welcome <? Php echo $ username; ;?>
        <a href="logout.php"> Logout </a>;
        ?>
 login.php
 <? php
        session_start ();
        include 'database.php';
       
        if (! empty ($ _ POST)) {
           
            $ username = $ _POST ['username'];
            $ password = md5 ($ _ POST ['password']);
       
            $ sql = "select * from user where username = '". $ username. "' and password = '". $ password. "'";
            #echo $ sql. "<br />";
            $ query = mysql_query ($ sql) or die (mysql_error ());
       
            // check for valid query
            if ($ query) {
                $ row = mysql_num_rows ($ query);
               
                // if $ row> 0 or username and password are found
                if ($ row> 0) {
                    $ _SESSION ['isLoggedIn'] = 1;
                    $ _SESSION ['username'] = $ username;
                    header ('Location: index.php');
                } else {
                    echo "wrong username or password";
                }
            }
        }
        ?>
       
        <form action = "" method = "post">
        <input type = "text" name = "username">
        <input type = "password" name = "password">
        <input type = "submit" value = "login">
        </ form>
 logout.php
<?php
    session_start();
    session_destroy();
    header('Location: login.php');
    ?>
database.php
?php
$host = "localhost";
$user = "root";
$pass = "root";
$db_name = "cms";

mysql_connect($host, $user, $pass) or die (mysql_error());
mysql_select_db($db_name) or die (mysql_error());
?>
3. Save this files in a folder called "login-form" and place the folder in htdocs folder.
     The htdocs folder is located in the XAMPP folder, in this tutorial we use XAMPP as the web server.

After doing all the steps how to make login form above, now it's time we to try login form with PHP and MySQL which completed session by typing address "http: //localhost/login-form/index.php" on browser page.

Similarly tutorial how to create login form, if there is step less understood, please send question through comment field below. Thanks.




Captcha

How to Create Captcha using PHP In this tutorial we will discuss How to Create Captcha Using PHP. Cahtcha which stands for Completely Aut...