Saturday, December 3, 2016

Login With Username Display In PHP and MySQL

I has been a while since I updated my website because of my heavy and  very hectic work schedule. Anyway guys I'm back in this article I would like to share with you a login system that is database driven in PHP and MySQL. This login system will identify the user's name after the user successfully login to the system by displaying the name of the user in the welcome page. Actually this problem it takes a while for me to solve this one it is a very simple SQL query just to retrieve the name of the user in the database I hope you will like my work. Feel free to use my code in your project that uses PHP and MySQL.  In this article I include the complete source code and it's complete database and table structure. Thank you.

Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com

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



Database and Table Structure


SQL Dumb File

users.sql

-- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Nov 28, 2016 at 11:42 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(200) NOT NULL,
  `password` varchar(200) NOT NULL,
  `lastname` varchar(200) NOT NULL,
  `firstname` varchar(200) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

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

INSERT INTO `users` (`id`, `username`, `password`, `lastname`, `firstname`) VALUES
(1, 'jake', 'jake', 'POMPERADA', 'JAKE'),
(2, '123', '123', 'POMPERADA', 'JACOB SAMUEL'),
(3, 'iya', 'iya', 'POMPERADA', 'JULIANNA RAE'),
(4, 'allie', 'allie', 'POMPERADA', 'MA. JUNALLIE'),
(5, 'bill', 'bill', 'GATES', 'WILLIAM'),
(6, 'peter', 'peter', 'NORTON', 'PETER');

--
-- 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=7;
/*!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 */;


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> 

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>
<h2> Login System </h2>
<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>

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; 
}
//check if the username entered is in the database.
$test_query = "SELECT * FROM users WHERE username = '".$_POST[username]."'";
$query_result = mysql_query($test_query);

// query to get the users lastname and firstname to be display in the main page
$test_query2 = "SELECT lastname,firstname FROM users WHERE  username = '".$_POST[username]."'";
$query_result2 = mysql_query($test_query2);

//conditions
if(mysql_num_rows($query_result)==0) {
//if username entered not yet exists
    echo "<font name='arial'> <font size='5'>";
    echo "<br>";
    echo "The username that you have given is invalid. Please try again.";
    echo "<br><br>";
    echo "<a href='logout.php'>Return To Login Page</a> " ;
    echo "</font></font>";
}else {
//if exists, then extract the password.
    while($row_query = mysql_fetch_array($query_result)) {
        // check if password are equal
        if($row_query['password']==$_POST['password']){
        
        while($row_query2 = mysql_fetch_array($query_result2)) {
  
            $_SESSION['username'] = $_POST['username'];
            // This two line of code will able us to retrieve the lastname
                 // and firstname of the user we just login.
                 $_SESSION['lastname'] = $row_query2['lastname'];
            $_SESSION['firstname']= $row_query2['firstname'];
                 header("Location: home.php");
                 exit;
            }  
        } else{ // if not a valid user  
            echo "<br>";
            echo "<h2> Invalid Password !!! Try Again </h2>"; 
            echo "<br>";
            echo "<font name='arial'> <font size='5'>";
            echo "<a href='logout.php'>Return To Login Page</a> " ;
            echo "</font></font>";
        }
    }
}
?>

</body>
</html>


logout.php

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

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





29 comments:

  1. Programming Source Codes And Computer Programming Tutorials: Login With Username Display In Php And Mysql >>>>> Download Now

    >>>>> Download Full

    Programming Source Codes And Computer Programming Tutorials: Login With Username Display In Php And Mysql >>>>> Download LINK

    >>>>> Download Now

    Programming Source Codes And Computer Programming Tutorials: Login With Username Display In Php And Mysql >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete