PHP MySQL Login System with Captcha - Web Development and Web Design Codes

Latest

Wednesday, May 31, 2017

PHP MySQL Login System with Captcha

PHP MySQL Login System with Captcha

PHP MySQL Login System with Captcha
PHP MySQL Login System with Captcha

Hi Guys Today we will Learn how to Create PHP MySQL Login System with captcha.
It's Simple Easy and Beginners Friendly. if You don't know how to implement captcha system in php login form please learn the below codes above..
Also Learn
1. PHP MySQL Pagination
2. PHP MySQL Login with SESSION
3. php mysql comment system
4. sort MySQL table column using PHP

5. PHP MySQL Shopping Cart

What you Will Need:

Database Creation:
Database Name: codenair
table name: user
Use The Below SQL Codes to Create user table
CREATE TABLE IF NOT EXISTS `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(150) NOT NULL,
  `password` varchar(150) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Now Insert Some Username and Password Manually.
files Creation:
Create 6 files called.
1. connect.php
2. captcha.php
3. index.php
4. profile.php
5. logout.php
6. style.css

Now copy or manually write the below codes for different file..
connect.php
Creating Connection Between Server and Database.
<?php
$conn=new mysqli('localhost','root','','codenair');
?>

captcha.php
Codes for Captcha.php to Generate Random Captcha.
<?php
$captcha=mt_rand(111,333);
$_SESSION['captcha_code']=$captcha;
echo $captcha;
?>

index.php
index page to Process Login Details and Validate captcha.
<?php
include("connect.php");
session_start();
//Checking User Already Logged or Not
if(!empty($_SESSION['user'])){
 header('location:profile.php');
}
if(isset($_POST['submit'])){
  //Validating Captcha Code
 if($_POST['captcha_code']==$_SESSION['captcha_code']){
   
  $name=$_POST['username'];
  $password=$_POST['password'];
     //Protect MySQL Injection
  $name=mysqli_real_escape_string($conn,$name);
  $password=mysqli_real_escape_string($conn,$password);
  $result=mysqli_query($conn,"SELECT*FROM user WHERE username='$name' and password='$password'");
  //Counting row
  //if UserName and PassWord Matched row must be 1
  $row=$result->num_rows;
  if($row==1){
   $user=$result->fetch_assoc();
       //Setting SESSION for Logged user
      $_SESSION['user']=$user; 
      header('location:profile.php');   
  }else{
   $inputerror ='UserName or PassWord is not Correct';
  }
 }else{
  $error= 'You Entered Invalid Captcha';
 }
}
?>
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css"/>
<title>PHP MySQL Login With Simple Math Captcha</title>
</head>
<div id="form">
<h2>PHP MySQL Login With Simple Math Captcha</h2>
<form method="post" action="">
 <table>
    <tr>
   <td><label>UserName:</label></td>
   <td><input type="text" name="username"/></td>
 </tr>
 <tr>
   <td><label>PassWord:</label></td>
   <td><input type="text" name="password"/></td>
 </tr>
 <tr>
   <td></td>
   <td><span><?php include('captcha.php');?></span></td>
 </tr>
 <tr>
   <td><label>Captcha:</label></td>
   <td><input type="text" name="captcha_code"/></td>
 </tr>
 <tr>
   <td></td>
   <td><input type="submit" name="submit" value="Login"/></td>
 </tr>
 </table>
<span><?php if(isset($inputerror)){echo $inputerror;}?></span>
<span><?php if(isset($error)){echo $error;}?></span>
</form>
</div>
</html>

4. profile.php
Successfully Logged user will Redirect to Profile page..
<?php
session_start();
if(!empty($_SESSION['user'])){
 ?>
  <link rel="stylesheet" type="text/css" href="style.css"/>
  <div id="form">
    <label>Your UserName: </label><span> <?php echo $_SESSION['user']['username'];?></span>||
    <label>Your PassWord: </label><span> <?php echo $_SESSION['user']['password'];?></span>
    <div><label>Your all Info:</label>  <span><?php print_r($_SESSION['user']);?></span></div>
     <a href="logout.php">Logout Now</a>
  </div>
<?php
}else{
 echo 'Your are not Logged in..please <a href=index.php>Login Now</a>';
}
?>

5. logout.php
Logout page to Destroy current logged user SESSION
<?php
session_start();
session_destroy();
//Redirecting User to index page after Destroy SESSION
header('location:index.php');
?>

6. style.css
add some CSS codes to Design our Index.php Page.
#form{
 width:600px;
 height:300px;
 background:#ddd;
 color:green;
 margin:auto;
}
input[type=text]{
 padding:7px;
 width:250px;
 border-radius:5px
}
input[type=text]:focus{
 border:2px solid black;
 padding:7px;
}
input[type=submit]{
 padding:5px 8px;
 font-weight:bold;
 border-radius:4px;
 background:#080808;
 color:white;
 cursor:pointer;
}
label{
 font-size:20px;
 color:black;
 font-weight:bold;
}
span{
 font-size:20px;
 color:red;
}

Yes,You Have Successfully Created PHP MySQL Login System With captcha.
Thank You for Visiting.

No comments:

Post a Comment

Thank You for Your Comment

Note: Only a member of this blog may post a comment.