Thursday, May 4, 2017

Simple Login System in PHP and MySQL

Here is a sample program wrote by my best friend and fellow software engineer Mr. Dave Marcellana a simple login system using PHP and MySQL. It uses bootstrap framework for the design. I hope you will find the work of Dave useful. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.



Sample Program Output



Program Listing

db.php


<?php
mysql_connect("localhost","root","") or
 die('Could not connect to the database!');


mysql_select_db("dbloginv1") or
 die('No database selected!');
?>

index.php

<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title Page</title>

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<h1 class="text-center">Login Version 1.0</h1>

<?php
session_start();

//Includes the connection file that contains the MYSQL database information
include('db.php');

if(isset($_POST['login']))
{
$user = $_POST['username'];
$pass = $_POST['password'];

// Selects the username and password from the users database.
$query = "SELECT * FROM users WHERE User='$user'";

$result = mysql_query($query);

if(!$result) 
{
echo "The query failed " . mysql_error();
else 
{
// If the row vairble does not equal the pass variable then an error occurs.
$row = mysql_fetch_object($result);

if(isset($_COOKIE['login']))
{
if($_COOKIE['login'] < 3)
{
$attempts = $_COOKIE['login'] + 1;
setcookie('login', $attempts, time()+60*10); //set the cookie for 10 minutes with the number of attempts stored
echo "I'm sorry, but your username and password don't match. Please go back and enter the correct login details.You Click <a href=\"login.php\">here</a> to try again.";
else
{
echo 'You\'ve had your 3 failed attempts at logging in and now are banned for 10 minutes. Try again later!';
}
else 
{
setcookie('login', 1, time()+60*10); //set the cookie for 10 minutes with the initial value of 1
}
exit;
header('Location: http://www.google.com');
}
}
?>

<form action="" method="POST" role="form">
<legend>Login</legend>
<div class="form-group">
<label for="">Username</label>
<input type="text" class="form-control" name="username" placeholder="Username" required="required">
</div>

<div class="form-group">
<label for="">Password</label>
<input type="password" class="form-control" name="password" placeholder="Password" required="required">
</div>
<button type="submit" name="login" class="btn btn-primary">Submit</button>
</form>

<!-- jQuery -->
<script src="//code.jquery.com/jquery.js"></script>
<!-- Bootstrap JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
  <script src="Hello World"></script>
</body>
</html>


No comments:

Post a Comment