Php Login System With Mysql Database

Share:

PHP Login System With MySql Database | PHP Login System.
Hello Readers...
In this article we are going to create a login system using HTML and PHP.

-First create a separate config.php file to create the connection with MySql Database.
-If we create a config.php file separately then we can use this file in all PHP pages where connection is required i.e.,we can reuse this file n number of times for Database Connection Purpose.
-Now create login.php file,this file will be responsible to to authenticate the users credentials and redirect then to welcome page.
-Assume our Database name is mysql@123 and Table name is login with username and password column.
So lets create config.php.

This file is responsible to establish the connection with MySql Database.
config.php
<?php
/* Assume we are running MySQL with default setting (user 'root' with no password) */
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'mysql@123');

/* Attempting to connect to MySQL database */
$con = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);

// Checking connection
if($con === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>

Now create a login.php file.
Here we are going to write PHP login logic inside login.php only.
In this file we have created a form with two text-field i.e., username and password to take input from the user's.

We have also created a submit button to submit the form after filling username and password.

When user hit the submit button after filling username and password then this login.php is responsible to authenticate the user credentials.

If user enter valid username and password then user will be redirected to welcome page otherwise a message will be displayed saying invalid login details,see below example.
login.php
<?php
  if($_SERVER["REQUEST_METHOD"] == "POST"){
     $username=$_POST['username'];  
     $password=$_POST['password']; 
     include('config.php');                             
     $sql_query = "select count(*) as cntUser from login where               username='".$username."' and password='".$password."'";
     $result = mysqli_query($con,$sql_query);
     $row = mysqli_fetch_array($result);
     $count = $row['cntUser'];

     
if($count > 0){ 
     echo "
     <script>
       alert('Success...'); 
       function redi(){window.location='welcome.php';}
       document.write('<font color=blue>'+'please wait while                     redirecting to Welcome page...'+'</font>');
       setTimeout('redi()',3000);
     </script>"; 
}
else{
    echo "
     <script>alert('Invalid Login Details...'); 
     </script>"; 
    }
?>
<html>
 <head>
  <title>Login Page</title>
</head>
<body>
<center>
<form method="post"  action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">

Username :<input type="text" name="username" id="username" required="required"/><br/>

Password :<input type="password" name="password" id="password" required="required"/><br/>

 <input type="submit"  value="Login"/> 
 </form>
</center>
 </body>

 </html>

Now create welcome.php.
If user entered valid username and password then login page will redirect the user's to this welcome.php
welcome.php
<html>
 <head>
     <title>
         Welcome Page
     </title>
 </head>
    <body><br/><br/>
    <center>
        Welcome User to our Welcome Page.
    </center>
    </body>
</html>

Hope this article was helpful.

No comments

You May Like Also

Programming Knowledge


Java JDK
Reversing String in Java
Java Generic Concept With Simple Example
Java Generic Objects Behavior In Non-Generic Area
Creating Connection With MySql Databse Using Java Application
Performing Database Operations Java MySql Insert Update Delete Select
Java Inserting Data in MySql Databse Table
Java Basic Login System Using MySql Database Table
JSP Login System Using MySql Databse
Jsp Servlet Login And Logout System Using Mysql Database Table
What is jdk in Java?
Exaplain inheritance in Java with example?
Exaplain polymorphism in Java with example?
Exaplain abstraction in Java with example?
Exaplain encapsulation in Java with example?
What is the use of this keyword in Java?
Explain ArrayList With Example
Explain LinkedList With Example
Explain Vector Class With Example
Explain Vector Class With Example
Generics in java with example
Behavior of generic objects
How can i pass generic class to a method in java
How to create a login system in jsp using mysql database?
How to create a login and logout system in jsp & servlet using mysql database?
How to connection a simple Java application with mysql database?
How to insert values in mysql databse usiong Java applications?
Perform insertion, retrieval, updatation and delete operations?
How to create a simple login system in java using mysql database?

PHP Login System Using MySql Database
PHP Complete Login System With Session And Logout Using MySql Database