PHP User Login With Captcha


index.php

<?php session_start(); 
if(isset($_SESSION['is_login'])){
header('Location:login.php');
    exit;
}
if (isset($_POST['submit'])) {
$getUser = $_POST['username'];
    $getPass = $_POST['userpass'];
    $getCaptcha = $_POST['captcha'];
    if($getUser=='demo' && $getPass=='demo' && ($getCaptcha==$_SESSION['captcha_text'])){
    $_SESSION['set_user'] = $getUser;
    $_SESSION['is_login'] = true;
header('Location:login.php');
    exit;
    } else {
    header('Location:index.php');
    exit;
    }
} else {
?>
 <!DOCTYPE html>
 <html>
 <head>
  <title>Login</title>
  <style type="text/css">
  *{
  font-family: arial;
  font-size: 12px;
  margin: 2px;
  }
  </style>
 </head>
 <body>
  <form method="post" action="index.php">
<h3>User Login</h3>
  Username : <input type="text" name="username"/><br/><br/>
  Password : <input type="text" name="userpass"/><br/><br/>
        <img src="captcha.php"/><br/><br/>
        <input type="text" name="captcha" placeholder="Type Code" /><br/><br/>
  <input type="submit" value="Login" name="submit">
  </form>
 </body>
 </html>
 <?php }
  ?>

login.php

<?php  
session_start();
if(!isset($_SESSION['is_login'])){
header('Location:index.php');
    exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Welcome Guest</title>
<style type="text/css">
  *{
  font-family: arial;
  font-size: 12px;
  }
  </style>
</head>
<body>
<h3>Welcome Guest <u><?= $_SESSION['set_user'] ?></u></h3>
<a href="logout.php">Logout</a>
</body>
</html>


captcha.php

<?php
session_start();
$alphabet = "ABCDEFGH";
$captcha_text = "";
for ($i=0; $i < 4; $i++) { 
$rand = rand(0,7);
$captcha_text.=substr($alphabet, $rand,1);
}
$_SESSION['captcha_text'] = $captcha_text;
header("Content-Type: image/png");
$im = @imagecreate(110, 20)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 10, 5, 2,  $captcha_text, $text_color);
imagepng($im);
imagedestroy($im);
?>


Demo 1

Demo 2


Share on Google Plus

About Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment