i want to use captcha to login

3 replies
hi i want use captcha in my admin login page
and with check the captcha can login ?
what should i do ?
this is my login page
Code:
session_start();
if ( isset($_POST['username']) && isset($_POST['password']) )
{
	include "config.php";
	if ( ($_POST['username'] == $adminuser) && ($_POST['password'] == $adminpass) )
	{
		$_SESSION['user'] = $_POST['username'];
		header ("Location: home.php");
	}
}
?>
   <div class="content">
	<div class="top-text">ورود به مدیریت</div> 
	<div class="forms">						
	<form method="post" action="">
	<label for="username"> نام کاربری : </label>
	<input name="username" type="text" value="" class="form" />
	<br  /><br  /><br  /><br  /><br  />
	<label for="Email"> رمز عبور : </label>
	<input name="password" type="password" class="form-ltr" value="" />
	<br /><br /><br /><br />
	<input type="submit" value="ورود به مدیریت" name="submit">
	</form>
	</div>


    </div>
	<div class="clear"></div>
</div>
</body></html>

and its captcha page
Code:
<!DOCTYPE html>
<html>
<script type="text/javascript">
var httpRequest;

function createHttpRequest()
{
	var ret;

	try
	{
		ret = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e)
	{
		ret = new XMLHttpRequest();
	}
	
	return ret;
}

function request(method, uri, params)
{
	if(!httpRequest)
		httpRequest = createHttpRequest();
	
	httpRequest.open(method, uri, true);
	if(params)
	{
		httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		httpRequest.setRequestHeader("Content-length", params.length);
	}
	httpRequest.setRequestHeader("Connection", "close");
	
	httpRequest.onreadystatechange = getRequest;
	httpRequest.send(params);
}

function getRequest()
{
	if(httpRequest.readyState == 4)
	{
		try
		{
			var txt = httpRequest.responseText;
			if(txt.indexOf('good') != -1)
			{
				document.getElementById('message').innerHTML = 'Correct';
			}
			else if(txt.indexOf('error') != -1)
			{
				document.getElementById('message').innerHTML = 'Error';
				captcha();
			}
			else
			{
				draw(txt);
			}
		}
		catch(e)
		{
			return false;
		}
	}
}

function draw(code)
{
	var canvas = document.getElementById('captcha');
				
	if(!canvas.getContext)
		return;
	
	var img, ctx = canvas.getContext('2d');

	if(ctx.createImageData)
		img = ctx.createImageData(140, 35);
	else if(ctx.getImageData)
		img = ctx.getImageData(0, 0, 140, 35);
	else
		img = {'width' : w, 'height' : h, 'data' : new Array(140 * 35 * 4)};
	
	eval(code);
	
	for(var j = 0; j < 35; j++)
	{
		for(var i = 0; i < 140; i++)
		{
			var idx = (i + j * 140) * 4;
			
			img.data[idx + 0] = colorArray[idx + 0];
			img.data[idx + 1] = colorArray[idx + 1];
			img.data[idx + 2] = colorArray[idx + 2];
			img.data[idx + 3] = colorArray[idx + 3];
		}
	}
	ctx.putImageData(img, 0, 0);
}

function captcha()
{
	request('GET', 'captcha.php?get', '');
}

function check_captcha()
{
	var cap = document.getElementById('cap').value;
	if(cap.length < 1)
	{
		alert('آâهنèٍه ٍهêٌٍ ٌ êàًٍèيêè.');
		return false;
	}
	request('POST', 'captcha.php', 'cap='+cap);
}
</script>
<body onLoad="captcha();">
	<table>
	<tr>
		<td>
			<canvas id="captcha" width="140" height="35" onClick="captcha();"></canvas>
		</td>
	</tr>
	<tr>
		<td>
			<form>
				<input type="text" id="cap" size="12">
				<input type="button" value="OK" onClick="check_captcha();">
			</form>
		</td>
	</tr>
	<tr>
		<td>
			<span id="message"></span>
		</td>
	</tr>
</body>
</html>
#captcha #login
  • Profile picture of the author Mkj
    Have you a static ip and admin is in it's own directory? If you have both you can protect it very easily with htaccess. If both the above are true I can give you the code for it. Very strong protection and less hassle.
    {{ DiscussionBoard.errors[6684856].message }}
  • Profile picture of the author jaasmit
    Why you need to use captcha in your admin login page. I am not clear about it.
    You should avoid this to make it more simple.
    You should avoid complexity.
    {{ DiscussionBoard.errors[6685760].message }}
  • Profile picture of the author isocertification
    Banned
    [DELETED]
    {{ DiscussionBoard.errors[6687287].message }}
    • Profile picture of the author javadth
      i want when user want to login check the captcha and if correct he can login
      {{ DiscussionBoard.errors[6687409].message }}

Trending Topics