Labels

php (35) javascript (31) phpjavascript (30) jquery (23) html (20) mysql (14) database (9) codeigniter (4) json (4) bar chart (2) calendar (2) column chart (2) framework (2) google maps (2) query (2) tables (2) url (2) dropdown (1)

Tuesday, February 27, 2018

Simple login form using PHP and MySQL.

The login form is very much essential in every kind of web application so here we are explaining a simple and best way of doing login system. which mainly includes 
1. Connecting to Database
2. Login(Session Start)
3. Logout(Session End)
4. Session Check
5. Login Form
6. CSS

1. Connecting to Database

$DatabaseServer = "localhost";
$DatabaseUsername = "root";
$DatabasePassword = "root";
$DatabaseName = "demo";

$Connection = mysqli_connect($DatabaseServer, $DatabaseUsername, $DatabasePassword, $DatabaseName);

if ($Connection === false) {
    die("ERROR: Could not connect. " . mysqli_connect_error());
}



2. Login

session_start();
if (isset($_POST['username']) and isset($_POST['password'])) {
   
    $Username = $_POST['username'];
    $Password = $_POST['password'];
    $query = "SELECT * FROM `user` WHERE FirstName='$Username' and Password='$Password'";
    $result = mysqli_query($Connection, $query) or die(mysqli_error($connection));
    $count = mysqli_num_rows($result);

    if ($count == 1) {
        $_SESSION['username'] = $
Username;
    } else {
        $fmsg = "Invalid Login Credentials.";
    }
}


3. Logout

session_start();
session_destroy();
header('Location: login.php');


4. Session Check

if (isset($_SESSION['username'])) {
    
    echo "<div class='session-box'>";
    echo 'Welcome Mr.'.$Username = $_SESSION['username'];
    echo "<br><a href='logout.php'>Logout</a>";
    echo "</div>";
}else{
    
    echo "<div class='session-box'>";
    echo "You are logged out.";
    echo "</div>";

}

5. Login Form

<form method="POST" class="login-box">

    <div class="form-header">Please Login</div>

    <div class="col-md-12">  
        <label>Username</label>
        <input type="text" name="username" class="form-control" placeholder="Username" required>
    </div>

    <div class="col-md-12">
        <label>Password</label>
        <input type="password" name="password" class="form-control" placeholder="Password" required>
    </div>

    <div class="col-md-6">
        <button class="btn btn-primary" type="submit">Login</button>
    </div>

</form>

6.CSS

 <head>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <style>
            .login-box{
                position: absolute;
                width: 300px;
                height: 270px;
                z-index: 15;
                top: 50%;
                left: 50%;
                margin: -100px 0 0 -150px;
                border: 1px solid #8080804a;
                border-radius: 5px;
                background: #8080804a;
            }
            .session-box{
                position: absolute;
                width: 300px;
                height: 100px;
                z-index: 15;
                top: 10%;
                left: 50%;
                margin: 0px 0 0 -150px;
                border: 1px solid #8080804a;
                border-radius: 5px;
                background: #8080804a;
                padding: 30px;
            }
            .form-control{
                padding: 10px;
                margin-top: 5px;
            }
            .btn-primary{
                margin-top: 20px;
            }
            .form-header{
                width: 299px;
                height:40px;
                background: #004f61;
                color:white;
                text-align: center;
                font-size: 20px;
                margin-bottom: 20px;
                padding: 5px;
            }
        </style>

    </head>

Create Database Table

CREATE TABLE `user` (
  `UserID` int(12) NOT NULL,
  `FirstName` varchar(48) NOT NULL,
  `LastName` varchar(48) NOT NULL,
  `Email` varchar(128) NOT NULL,
  `Password` varchar(20) NOT NULL,
  `City` varchar(48) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


INSERT INTO `user` (`UserID`, `FirstName`, `LastName`, `Email`, `Password`, `City`) VALUES
(7, 'Rahul', 'Rajshekaran', 'Rahul@zzz.xxx', 'Rahul@123', 'Pune'),
(8, 'Mahesh', 'Krishna', 'Mahesh@xxx.xxx', 'Mahesh@123', 'Delhi');







Result




20 comments:

  1. You can actively talk with our experts through Staples Chat Support. They will help you 24/7 to address all your device problems.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Book Flights to Nashville. Get Cheap flights to Nashville Guaranteed on a huge option of flights deals to Nashville. Grab this attractive offer now.

    ReplyDelete
  4. Wonder in the new place full of museums and parks by taking Flights From New York to Las Vegas. Get your bookings done with our travel agents and make your next journey adventurous. Book soon to get early bird offers.

    ReplyDelete
  5. Extremely common to handle banking errors like QuickBooks Error code 9999. When such an error takes place, the machine tends to freeze for a couple of seconds on repeat. This example can obviously affect business awfully. If you want to Fix QuickBooks Error 9999 then you may contact our ProAdvisors.

    ReplyDelete
  6. You are able to download Office.com/setup com setup on the internet for receiving the benefits of word, excel and other utilities that are extremely helpful for documentation and also for company purposes. We give you online assistance for each and every sort of problems and assistance from Office.
    Office.com/setup

    ReplyDelete
  7. Hi there, just became aware of your blog through Google, and found that it's really informative. I’m going to watch out for brussels. I will be grateful if you continue this {in future}. Many people will be benefited from your writing. Cheers! nature's way Singapore

    ReplyDelete
  8. Air China Airlines Customer Service is available 24*7 to help you. Reach experts at Air China Airlines Customer Service Phone Number now for queries.

    ReplyDelete
  9. This is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. app development services

    ReplyDelete
  10. Wow! Thank you! I always needed to write on my website something like that. Can I implement a part of your post to my site? events backdrop

    ReplyDelete
  11. Sweet blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! List of Accounting & Tax Companies in Singapore

    ReplyDelete
  12. Best Deals at Safemartshop, an international prescription service provider, which contracts with International dispensaries and USA pharmacies, is a leader in referring orders for prescription and non-prescription medications on behalf of customers throughout the world
    Safemartshop
    buy hydrocodone online
    buy oxycodone online
    buy oxycontin online
    buy percocet online
    buy vicodin online
    buy norco online
    buy adderall online
    buy xanax online

    ReplyDelete
  13. If you are QuickBooks user and getting QuickBooks error 2107 then you can read the post and fix it or Call Quickbooks Helpline Number 1-800-242-0792.
    QuickBooks error 2107

    Fix QuickBooks error

    Quickbooks Helpline Number

    Quickbooks Support Number

    ReplyDelete
  14. If you suddenly find yourself in Facebook jail and you don't even know how this happened, then here you will get detailed information on how to get yourself out of Facebook jail.

    blocked from posting on Facebook
    Facebook Jail
    how long does Facebook block you
    get out from Facebook Jail

    ReplyDelete
  15. Intuit has recommended some solutions which could correct the error with expert guidance from QuickBooks Error 15243 ProAdvisors. Stick to the resolution steps within the given sequence so your best results can be obtained.

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. QuickBooks Error 6175 is an easy and trustworthy program for almost any business now each day. QuickBooks program can handle data that is employed by every business to produce their venture, track their records and in addition tracks their transaction.

    ReplyDelete
  18. There are various other reasons as well that are mentioned below. Methods to resolve the QuickBooks Error 83 are mentioned you'll want to opt in accordance with the reason associated with problem.

    ReplyDelete

More Posts

FOLLOW