PDA

View Full Version : Bác nào biết về captcha thì cho mình hỏi cái này với



hackerstudent
18-10-2009, 07:17 PM
Mình có 1 trang admincp, hiện tại mình đang muốn làm phần nhập mã xác nhận mới cho đăng nhập. Mình có search trên google và tìm được 1 code captcha nhưng chưa biết phải add vào web của mình ra sao. Bác nào biết thì giúp mình với. Đây là trang admincp/index.php; admincp/login.php; admincp/captcha/CaptchaSecurityImages.php & 1 code nó bảo lưu vào để kiểm tra, các bạn đọc mà biết thì giúp mình với. Cảm ơn trước.

Index.php

<?php
session_start();
define('IN_TEENNET', true);
define('IN_TEENNET_ADMIN', true);
include('../function/config.php');
include('../function/fun.php');
include('../function/string.php');
include('../function/admin_fun.php');
include('../function/resize.php');
?>
<html>
<head>
<title>Trang quan ly</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="style.css" type=text/css>
<script language="javascript" src="js/admin.js"></script>
<script language="javascript" src="../js/mudim.js"></script>
<script language="javascript" src="../js/wysiwyg/wysiwyg.js"></script>
</head>

<body>
<div align="center">
<?php
$level = $_SESSION['admin_level'];
if (!$level) {
$error = $_GET['error'];
?>
<br>
<form method="post" action="login.php">
<table width="25%" cellpadding="2" cellspacing="0" class="table">
<tr><td colspan="2" align="center" class="t_cat">Đăng nhập AdminCP</td></tr>
<?php if($error=="u"){ ?>
<tr><tr><td width="25%" style="padding-top:5px;" align="right"><img src="images/error.gif"></td><td style="padding-top:5px;"><div class="error" style="padding-top:3px;">Sai tài khoản hoặc mật khẩu.</div></td></tr><?php }else{ ?><?php } ?>
<tr><td align="right">Tài khoản:</td><td><input name="user_name" type="text" size="25"></td></tr>
<tr><td align="right">Mật khẩu:</td><td><input name="user_password" type="password" size="25"><img src="captcha/CaptchaSecurityImages.php?width=100&height=40&characters=5" /><br />
<label for="security_code">Security Code: </label><input id="security_code" name="security_code" type="text" /></td></tr>
<tr><td colspan="2" align="center" style="padding-bottom:5px;"><input type="submit" name="submit" class="btndangnhap" value="&nbsp;"></td></tr>
</table>
</form>
<?php
}
else{
$link = 'index.php';
if ($_SERVER["QUERY_STRING"]) $link .= '?'.$_SERVER["QUERY_STRING"];
?>
<table width="900px" border="0" cellpadding="0" cellspacing="4">
<tr><td colspan="2" class="banner">Control Panel</td></tr>
<tr><td width="20%" valign="top">
<table cellpadding="1" cellspacing="0" border="0" width="100%"><tr><td class="table"><?php include('menu.php'); ?></td></tr><tr><td style="padding-top:5px; padding-left:15px;"><a href="logout.php"><b>Logout</b></a></td></tr></table>
</td>
<td width="80%" valign="top">
<table cellpadding="1" cellspacing="0" border="0" width="100%"><tr><td class="table"><?php include('content.php'); ?></td></tr></table>
</td></tr>
<tr><td colspan="2" class="footer">Copyright (c) 2009 by <b>[W]-News</b><br> (<b>WoW-Teen.Com</b>)</td></tr>
</table>
<?php
}
?>
</div>
</body>
</html>

Login.php

<?php
session_start();
define('IN_TEENNET', true);
include('../function/config.php');
include("../function/fun.php");
if (isset($_POST['submit'])) {
$user_name = trim($_POST['user_name']);
$user_name = str_replace('|', '&#124;', $user_name);
$user_password = md5(stripslashes($_POST['user_password']));
$q = mysql_query("SELECT * FROM ".$table."user WHERE user_name = '".$user_name."' AND user_password = '".$user_password."' AND (user_level = 1 OR user_level = 2)");
if (mysql_num_rows($q)) {
$r = mysql_fetch_array($q);
$_SESSION['admin_id'] = $r['user_id'];
$_SESSION['admin_level'] = $r['user_level'];
echo "<meta http-equiv='refresh' content='0;url=index.php'>";
}
else {echo "<meta http-equiv='refresh' content='0;url=index.php?error=u'>";
}
}
?>


CaptchaSecurityImages.php

<?php
session_start();

/*
* File: CaptchaSecurityImages.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 03/08/06
* Updated: 07/02/07
* Requirements: PHP 4/5 with GD and FreeType libraries
* Link: http://www.white-hat-web-design.co.uk/articles/php-captcha.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/

class CaptchaSecurityImages {

var $font = 'monofont.ttf';

function generateCode($characters) {
/* list all possible characters, similar looking characters and vowels have been removed */
$possible = '23456789bcdfghjkmnpqrstvwxyz';
$code = '';
$i = 0;
while ($i < $characters) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
return $code;
}

function CaptchaSecurityImages($width='120',$height='40',$c haracters='6') {
$code = $this->generateCode($characters);
/* font size will be 75% of the image height */
$font_size = $height * 0.75;
$image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
/* set the colours */
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 20, 40, 100);
$noise_color = imagecolorallocate($image, 100, 120, 180);
/* generate random dots in background */
for( $i=0; $i<($width*$height)/3; $i++ ) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
}
/* generate random lines in background */
for( $i=0; $i<($width*$height)/150; $i++ ) {
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
}
/* create textbox and add text */
$textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
/* output captcha image to browser */
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
$_SESSION['security_code'] = $code;
}

}

$width = isset($_GET['width']) ? $_GET['width'] : '120';
$height = isset($_GET['height']) ? $_GET['height'] : '40';
$characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6';

$captcha = new CaptchaSecurityImages($width,$height,$characters);

?>

Mã kiểm tra:

<?php
session_start();
if(($_SESSION['security_code'] == $_POST['security_code']) && (!empty($_SESSION['security_code'])) ) {
// Insert you code for processing the form here, e.g emailing the submission, entering it into a database.
unset($_SESSION['security_code']);
} else {
// Insert your code for showing an error message here
}
?>