PHP MySQL Login System with Captcha - Web Development and Online Earning

Breaking

Web Development and Online Earning

Learn Web Development ,PHP MySQL,Codeigniter,AJAX,HTML CSS,Javascript,Jquery,Web Hosting,Online Earning,SEO,Link Building,Online Banking,Online Earning,Wordpress,Windows Tips,Web Design Tutorials...

test banner

Post Top Ad

Responsive Ads Here

Post Top Ad

Responsive Ads Here

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.

What you Will Need:

Database Creation:
Database Name: tutorial
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 AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

Now Insert Some Username and Password Manually.
files Creation:
Create 4 files called.
1.connect.php
2.captcha.php.
3.index.php
4.style.css
PHP MySQL Login System with Captcha
PHP MySQL Login System with Captcha

Now Copy The belowm codes for Certain file.
connect.php
Creating Connection Between Server and Database.
<?php
$host="localhost"; //Host Name
$username="root"; //MySQL Username
$password=""; //MySQL Password
$dbname="tutorial"; //Database Name
//Connect To The Server
mysql_connect("$host","$username","$password");
//select database
mysql_select_db("$dbname");
?>

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

index.php
Add the Below PHP codes top of the index page to Process Login Details and Validate Captcha.
<?php
include("connect.php");
session_start();
if(isset($_POST['submit'])){
 
 if($_POST['captcha_code']==$_SESSION['captcha_code']){
  
  $name=$_POST['username'];
  $password=$_POST['password'];
     //Protect MySQL Injection
  $name=stripslashes($name);
  $name=mysql_real_escape_string($name);
  $name=htmlspecialchars($name);
  
  $password=stripslashes($password);
  $password=mysql_real_escape_string($password);
  $password=htmlspecialchars($password);
  //Query Into Table by Passing UserName and PassWord.
  //If UserName and PassWord Matched row Must be 1
  
  $sql="SELECT*FROM user WHERE username='$name' and password='$password'";
  $result=mysql_query($sql);
  //Count row
  $row=mysql_num_rows($result);
  if($row==1){
   //Do Something
   $success ='You Have Successfully LoggedIn';
  }else{
   $inputerror ='UserName or PassWord Does Not Matched';
  }
 }else{
  $error= 'You Entered Invalid Captcha';
 }
}
?>

Now add The Below HTML codes after PHP codes to Display Login form and Captcha Codes.
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css"/>
<title>PHP MySQL Login With Simple Math Captcha</title>
</head>
<div id="form">
<form method="post" action="">
<h2>PHP MySQL Login With Simple Math Captcha</h2>
<label>UserName:</label><br/>
<input type="text" name="username"/><br/>
<label>PassWord:</label><br/>
<input type="text" name="password"/><br/>
<label>Captcha:</label><br/>
<span><?php include('captcha.php');?></span><br/>
<input type="text" name="captcha_code"/>
<br/>
<input type="submit" name="submit" value="Login"/>
<br/>
<div id="success"><?php if(isset($success)){echo $success;}?></div>
<span><?php if(isset($inputerror)){echo $inputerror;}?></span>
<span><?php if(isset($error)){echo $error;}?></span>
</form>
</div>
</html>

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

Yes,You Have Successfully Created PHP MySQL Login System With Captcha.
Thank You for Visit.

No comments:

Post a Comment

Thank You for Your Comment

Post Top Ad

Responsive Ads Here