Saturday, February 25, 2017

Three Attempts Login System in PHP and MySQLi OOP Version

Here is the another version of my code in PHP and MySQLi in Object Oriented Programming version because of the new version of PHP 7 the older commands in MySQL is already been depreciated or not a part of the language itself. The code is allow the user to login and has the ability to login only in three times if the user fail the application will be lock for security reason. The code is very short and easy to understand.


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 and Database Structure



Program Listing

connect_to_database.php<?php
     define('_HOST_NAME','localhost');
     define('_DATABASE_NAME','login');
     define('_DATABASE_USER_NAME','root');
     define('_DATABASE_PASSWORD','');
   
     $MySQLiconn = new MySQLi(_HOST_NAME,_DATABASE_USER_NAME,_DATABASE_PASSWORD,_DATABASE_NAME);
   
     if($MySQLiconn->connect_errno)
     {
         die("ERROR : -> ".$MySQLiconn->connect_error);
     }

     ?>


login_process.php


 
<html>
<body>
<style>
body {
    background-color: lightgreen;
    font-family:arial;
    font-size:20px;
}
</style>
<?php
error_reporting(0);
include 'connect_to_database.php'; //connect the connection page
 
if(empty($_SESSION)) // if the session not yet started
   session_start();
if(!isset($_POST['submit'])) { // if the form not yet submitted
   header("Location: login.php");
   exit;
}
      $attempt = 1;
      $myusername = $_POST['username'];
      $mypassword = $_POST['password'];
     
// query to get the users lastname and firstname to be display in the main page

$test_query2 = "SELECT * FROM users WHERE username = '$myusername' and password = '$mypassword'";

   
$res = $MySQLiconn->query($test_query2);

             if($res->num_rows > 0) {
                        while($row=$res->fetch_array()) {

                        $_SESSION['username'] = $_POST['username'];
                        $_SESSION['lastname'] = $row['lastname'];
                        $_SESSION['firstname']= $row['firstname'];
                        header("Location: home.php");
                        exit;
                    }  
        
            } else {
                    $number = $_SESSION['number'];
                    $number++;
                    $_SESSION['number'] = $number;
                    echo "<br><br>";
                    echo "<h1> Access Denied !!! Try Again </h1>";  
                    echo "Attempt Number   : <font color='red'> $number </font>";          
                    echo "<br><br>";
                    echo "<a href='login.php'>Return To Login Page</a> " ;
                    echo "</font></font>";
          
                if ($number>2) {
                    header("Location: lock.php");
                    exit;
                }
        }
            
?>
</body>
</html>

home.php
<?php
include 'connect_to_database.php'; //connect the connection page

if(empty($_SESSION)) // if the session not yet started
   session_start();
in.php");// send to login page
   exit;
if(!isset($_SESSION['username'])) { //if not yet logged in
   header("Location: log
    background-color:
}
?>
<html>
<body>
<style>
body {lightgreen;
    font-family:arial;
    font-size:20px;
    }
input, button, select, option, textarea {
    font-size: 100%;
}
</style>
<br>
<H2> Welcome Page </H2>
<br>
Welcome  <b> <?php echo $_SESSION['firstname']. " ".$_SESSION['lastname']."."; ?>  </b>
<br><br>
 <a href="logout.php">Logout</a>
</body>
</html>


lock.php
<html>
 <head>
 <title> Login Lock </title>
 </head>
<body>
<style>
body {
    background-color: lightgreen;
    font-family:arial;
    font-size:20px;
}
</style>

<?php
        
 $message1= "Sorry you are reached the three number of attempts to login in the system.";
 $message2= "The system will be locked.";
 $message3 = "Kindly send an email to the programmer for assistance at
           <font color='blue'> jakerpomperada@gmail.com </font>.";
            

 echo "<br><br>";
 echo "$message1";
 echo "<br>";
 echo "<h1><font color='red'> $message2 </font></h1>";
 echo "<br>";
 echo "<h3>$message3</h3>";
 echo "<br>";

 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href='logout.php'>CLOSE</a>";
?>
   </body>
</html>


logout.php
<?php
session_start();
unset($_SESSION['username']);
session_destroy();

header("Location: login.php");
exit;
?>

login.php
<?php
include 'connect_to_database.php'; //connect the connection page
if(empty($_SESSION)) // if the session not yet started
   session_start();


if(isset($_SESSION['username'])) { // if already login
   header("location: home.php"); // send to home page
   exit;
}

?>
<html>
<head></head>
<body>
<style>
body {
    background-color: lightgreen;
    font-family:arial;
    font-size:20px;
}
   input, button, select, option, textarea {
    font-size: 100%;
}

</style>
<br><br>
<h2> Three Attempt Login System in PHP and MySQLi OOP Version </h2>
<h4> Created By: Mr. Jake Rodriguez Pomperada, MAED-IT</h4>
<br>
<form action = 'login_process.php' method='POST'>
  Enter   Username:   &nbsp;
 <input type="text" name="username" />  <br><br>
    Enter Password: &nbsp;
 <input type="password" name="password" />
<br> <br>
<input type = "submit" name="submit" value="Ok" /> 
</form>
</body>
</html>

user.sql
-- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Feb 25, 2017 at 11:23 AM
-- Server version: 10.1.16-MariaDB
-- PHP Version: 5.6.24

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `login`
--

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `username` varchar(100) NOT NULL,
  `password` varchar(100) NOT NULL,
  `lastname` varchar(200) NOT NULL,
  `firstname` varchar(200) NOT NULL,
  `status` varchar(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`id`, `username`, `password`, `lastname`, `firstname`, `status`) VALUES
(1, 'jake', 'jake', 'POMPERADA', 'JAKE', '1'),
(2, 'jacob', 'jacob', 'POMPERADA', 'JACOB SAMUEL', '1'),
(3, 'allie', 'allie', 'POMPERADA', 'MA. JUNALLIE', '1'),
(4, 'iya', 'iya', 'POMPERADA', 'JULIANNA RAE', '1');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;



 DOWNLOAD SOURCE CODE HERE




























 

Three Attempt Login System in PHP and MySQL

Hi guys it has been a while since I want to create a login system in PHP and MySQL that limits the amount of attempt of login of the user. Here is it the complete code from start to finish I will allow the user to login just only for three attempt after that our login system will lock the application. The code is very easy to understand and use I hope you will find it useful.  Thank you very much. 

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 and Database Structure


Program Listing

connect_to_database.php

<?php
mysql_connect("localhost","root","") or die(mysql_error()); 
mysql_select_db("login");
?> 

home.php

<?php
include 'connect_to_database.php'; //connect the connection page

if(empty($_SESSION)) // if the session not yet started 
   session_start();

if(!isset($_SESSION['username'])) { //if not yet logged in
   header("Location: login.php");// send to login page
   exit;
?>
<html>
<body>
<style>
body {
    background-color: lightgreen;
    font-family:arial;
    font-size:20px;
    }
input, button, select, option, textarea {
    font-size: 100%;
}
</style>
<br>
<H2> Main Page </H2>
<br>
Welcome  <b> <?php echo $_SESSION['firstname']. " ".$_SESSION['lastname']."."; ?>  </b>
<br><br>
 <a href="logout.php">Logout</a> 
</body>
</html> 

lock.php

<html>
 <head>
 <title> Login Lock </title>
 </head>
<body>
<style>
body {
    background-color: lightgreen;
    font-family:arial;
    font-size:20px;
}
</style>

<?php
         
 $message1= "Sorry you are reached the three number of attempts to login in the system.";
 $message2= "The system will be locked.";
 $message3 = "Kindly send an email to the programmer for assistance at 
           <font color='blue'> jakerpomperada@gmail.com </font>.";
             

 echo "<br><br>"; 
 echo "$message1";
 echo "<br>";
 echo "<h1><font color='red'> $message2 </font></h1>";
 echo "<br>"; 
 echo "<h3>$message3</h3>";
 echo "<br>"; 
 
 echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href='logout.php'>CLOSE</a>";
?>


login.php

<?php
include 'connect_to_database.php'; //connect the connection page
if(empty($_SESSION)) // if the session not yet started 
   session_start();


if(isset($_SESSION['username'])) { // if already login
   header("location: home.php"); // send to home page
   exit; 
}

?>
<html>
<head></head>
<body>
<style>
body {
    background-color: lightgreen;
    font-family:arial;
    font-size:20px;
}
   input, button, select, option, textarea {
    font-size: 100%;
}

</style>
<br><br>
<h2> Three Attempt Login System in PHP and MySQL </h2>
<h4> Created By: Mr. Jake Rodriguez Pomperada, MAED-IT</h4>
<br>
<form action = 'login_process.php' method='POST'>
  Enter   Username:   &nbsp;
 <input type="text" name="username" />  <br><br>
    Enter Password: &nbsp;
 <input type="password" name="password" />
<br> <br>
<input type = "submit" name="submit" value="Ok" />  
</form>
</body>
</html>
  </body>
</html>

login_process.php

<html>
<body>
<style>
body {
    background-color: lightgreen;
    font-family:arial;
    font-size:20px;
}
</style>
<?php
error_reporting(0);
include 'connect_to_database.php'; //connect the connection page
  
if(empty($_SESSION)) // if the session not yet started 
   session_start();
if(!isset($_POST['submit'])) { // if the form not yet submitted
   header("Location: login.php");
   exit; 
}
  $attempt = 1;
      $myusername = $_POST['username'];
      $mypassword = $_POST['password'];
// query to get the users lastname and firstname to be display in the main page

$test_query2 = "SELECT * FROM users WHERE username = '$myusername' and password = '$mypassword' and status = 1";
$query_result2 = mysql_query($test_query2);
$rows = mysql_num_rows($query_result2);

$test_query3 = "SELECT * FROM users WHERE username = '$myusername' and password = '$mypassword' and status =0";

$query_deactive= mysql_query($test_query3);
$rows2 = mysql_num_rows($query_deactive);

            if($rows==1) {
                        
                        while($row_query2 = mysql_fetch_array($query_result2)) {
                        $_SESSION['username'] = $_POST['username'];
                        $_SESSION['lastname'] = $row_query2['lastname'];
                        $_SESSION['firstname']= $row_query2['firstname'];
                        header("Location: home.php");
                        exit;
                    }   
          
            } else {
                    $number = $_SESSION['number'];
                    $number++;
                    $_SESSION['number'] = $number;
                    echo "<br><br>";
                    echo "<h1> Access Denied !!! Try Again </h1>";  
                    echo "Attempt Number   : <font color='red'> $number </font>";           
                    echo "<br><br>";
                    echo "<a href='login.php'>Return To Login Page</a> " ;
                    echo "</font></font>";
            
                if ($number>2) {
                    header("Location: lock.php");
                    exit;
                }
        }
            
?>
</body>
</html>

logout.php

<?php
session_start();
unset($_SESSION['username']);
session_destroy();

header("Location: login.php");
exit;
?>

users.sql

-- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Feb 25, 2017 at 11:23 AM
-- Server version: 10.1.16-MariaDB
-- PHP Version: 5.6.24

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `login`
--

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `username` varchar(100) NOT NULL,
  `password` varchar(100) NOT NULL,
  `lastname` varchar(200) NOT NULL,
  `firstname` varchar(200) NOT NULL,
  `status` varchar(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`id`, `username`, `password`, `lastname`, `firstname`, `status`) VALUES
(1, 'jake', 'jake', 'POMPERADA', 'JAKE', '1'),
(2, 'jacob', 'jacob', 'POMPERADA', 'JACOB SAMUEL', '1'),
(3, 'allie', 'allie', 'POMPERADA', 'MA. JUNALLIE', '1'),
(4, 'iya', 'iya', 'POMPERADA', 'JULIANNA RAE', '1');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;



Login System With Three Times Attempt in Visual Basic 6 and Microsoft Access

In this article I would like to share with you guys the work of my best friend and fellow software engineer Mr. Dave Marcellana from Talisay City, Negros Occidental. This program we called Login System with Three Times Attempt in Visual Basic 6 and Microsoft Access which allows the user to login and it will stop the user if the given username and password is not correct in the three attempts that the user committed to the application. The program code is very simple and short very easy to understand and user. 

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



Directory Structure




Database and Table Structure


Program Listing


Form1

Private Sub Text2_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
   If modUserLogin.UserLogin(Text1.Text, Text2.Text) = True Then
           
      MsgBox "Welcome " & rs!Role & "!", vbInformation + vbOKOnly, "Access Granted"
    End If
End If
End Sub


modMain.bas

Public rs As New ADODB.Recordset
Public cn As New ADODB.Connection
Public sql As String
Public dbpath As String

Sub main()
Set rs = New ADODB.Recordset
Set cn = New ADODB.Connection

With rs
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
.LockType = adLockOptimistic
End With

dbpath = App.Path & "\database\database.mdb"

cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & dbpath & ";Persist Security Info=False"
Form1.Show
End Sub

modUserLogin.bas

Public Function UserLogin(user As String, pass As String) As Boolean
If rs.State = 1 Then rs.Close

    sql = "SELECT * FROM tblSecurity WHERE Username='" & user & "'" & "AND Password ='" & pass & "'"
    rs.Open sql, cn
    'rs.Open sql, cn
    
If Not rs.EOF Then
    If rs!Role = "Administrator" Then
        UserLogin = True
    Else
        UserLogin = True
    End If
Else
    'frmLogin.fraError.Visible = True
    MsgBox "Wrong Username or Password! Try Again!", vbExclamation + vbOKOnly, "Access Denied"
    UserLogin = False
    Form1.lblcount.Caption = Form1.lblcount.Caption + 1
    
    a = Form1.lblcount.Caption

    If a > 2 Then
    MsgBox "Sorry! You've reached the maximum retries for entering a user account. The system will be closed.", vbCritical + vbOKOnly, "Access Denied"
    End
    End If

End If
End Function




Tuesday, February 21, 2017

Swap of Two Numbers Without Using Third Variable in Java


In this article I would like to share with you a program that will ask the user to give two integer numbers and then our program will swap or interchange the arrangement of two numbers without using third variable in Java. The code is very easy simple and easy to understand. I am using TextPad as my text editor in this sample program.  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

swap.java


import java.util.Scanner;


class swap {

public static void main(String args[])
{

        int num1=0, num2=0;

        Scanner s = new Scanner(System.in);

        System.out.println();
        System.out.println("=== Swap of Two Numbers Without Using Third Variable in Java === ");
        System.out.println();

         System.out.print("Enter First Value : ");
         num1 = s.nextInt();

         System.out.print("Enter Second Value : ");
         num2 = s.nextInt();

         System.out.println();
 System.out.println("===== BEFORE SWAPPING ======");
         System.out.println();
         System.out.println(num1 + "      "  + num2);

         num1 = (num1+num2);
         num2 = (num1-num2);
         num1 = (num1-num2);

         System.out.println();
    System.out.println("===== BEFORE SWAPPING ======");
         System.out.println();
         System.out.println(num1 + "      "  + num2);

         System.out.println();
         System.out.println("===== END OF PROGRAM ======");
         System.out.println();

      }
}  // End of Code




Display String in Java

In this article I would like to share with you guys a program that I wrote in Java to display a string values from a string variable that I have assigned with the code was designed for beginners that are very new in Java programming in general. I am using TextPad as my text editor in this sample program. I hope you will like it. 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


class display_str {

public static void main(String args[])
{

   String title = "Computer Programming is Fun and Interesting.";

            System.out.println("\n\n");
   System.out.println("\t"+title);
   System.out.println();
   System.out.print("\t\t"+"=====  END OF PROGRAM =====");
           System.out.println("\n\n\n");

    }

}
  // End of Code



Sunday, February 19, 2017

Getline in C++

In this article I would like to share with you a sample program that I wrote that shows you how to use getline command in C++ to get input value from our user.

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

My mobile number here in the Philippines is 09173084360.


Program Listing

#include <iostream>
#include <string>

using namespace std;

 main() {

  string address,name;

  cout << "Name ";
  cin >> name;
  cout << "Enter your complete address : ";
   cin.ignore(80,'\n');
   getline(cin, address);

 cout << "\n\n";
 cout << "Hello " << name;
 cout << "\n";
 cout << "You address is ";
 cout << address;
   cout << "\n\n";
   system("PAUSE");
 }

Pointer Address in C++

A simple program that I wrote a long time ago in C++ that shows how to use pointers to know the memory address of the variable. The code is very short and easy to understand.

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

My mobile number here in the Philippines is 09173084360.


Program Listing

#include <iostream>

using namespace std;

main() {
    int value = 10;

    int *ptrvalue;

    ptrvalue = &value;

    cout << "The original value is " << value;
    cout << "\n\n";
    *ptrvalue = 20;
    cout << "The new value is " << value;

    cout << "\n\n";
    system("PAUSE");
}

Factors of a Numbers in PHP

Hi there in this article I would like to share with you a sample program that will compute the factors of a given number in PHP. The code is very short and easy to understand. 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



<html>
<title> Factors of a Number </title>
<style>
body {
 font-family:arial;
 font-size:20px;
}
</style>
<body bgcolor="lightgreen">
<br> <br>
<?php
$val = $_POST['number'];

?>
<h3> Factors of a Number in PHP </h3>
<form method="POST" action="">
    <input type="text" name="number" value="<?php echo $val;?>" placeholder="Give a Number"></input>
    <br> <br>
    <input type="submit" value="OK"></input> &nbsp;&nbsp;&nbsp;
     <input type="submit" name="clear" value="Clear"></input>
</form>
<?php

  if(isset($_POST['clear']) && !empty($_POST['clear'])) {
   $display = " ";
   $val = " ";
   $a=" ";
   }
   
if(isset($_POST['number']) && !empty($_POST['number'])) {
         
       $display = "The Factors of $val is ";
       echo $display;
     for($a=1; $a <=$val; $a++)
    {
        if ($val%$a == 0)
        {
           
            echo  " ".$a." ";
        }
        }
 }             

?>
</body>
</html>