How to Create a Captcha Image in PHP

Pagination with JqueryPagination is used when your data has exceeded in the page. This method will limit the data and set how many pages that will appear in the document of the page. Most of the time, the page is refreshing using this process but with the use of jQuery, there’s no need to load the page.

 

Lets begin:

1.Create a MySQL database and name it “peopledb“.

2.Do this following query for creating a table in the MySQL database.

[mysql]

CREATE TABLE `peopledb`.`tblpeople` (
`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`NAME` VARCHAR( 30 ) NOT NULL ,
`ADDRESS` VARCHAR( 60 ) NOT NULL ,
`SEX` VARCHAR( 11 ) NOT NULL ,
`CONTACT` VARCHAR( 15 ) NOT NULL
) ENGINE = INNODB;

[/mysql]

3.Create a file for a configuration between MySQL database and PHP script. Name it “config.php”.

[php]

$server = ‘localhost’;
$dbuser = ‘root’;
$dbpass = ”;
$dbname = ‘peopledb’;
$con = mysql_connect($server, $dbuser, $dbpass);
if (isset($con)) {
# code…
$dbSelect = mysql_select_db($dbname);
if (!$dbSelect) {
echo “Problem in selecting database! Please contact administraator”;
die(mysql_error());
}
} else {
echo “Problem in database connection! Please contact administraator”;
die(mysql_error());
}

[/php]

4.Create a css file for the layout of your design. Name it “style.css”.

[css]

.table {
width: 100%;
max-width: 100%;
margin-bottom: 20px;
}
.table > thead > tr > th,
.table > tbody > tr > td {
padding: 8px;
line-height: 2;
vertical-align: top;
border-top: 1px solid #eee;
}
.table > thead > tr > th {
text-align: left;
vertical-align: bottom;
border-bottom: 1px solid #eee;
}

[/css]

2.Do this following code for the index.

[php]

<?php include ‘config.php’; ?>

<!– css extension –>
<link rel=”stylesheet” type=”text/css” href=”css/style.css”>

<!– container –>
<div id=”wrap”>
<!– header –>
<h1 class=”page-header”>Pagination with Jquery, PHP and MySQL Database</h1>
<!– Load content –>
<div id=”content”> </div>
<!– for paging –>
<?php
$perPage = 3;

$sqlQuery = “SELECT * FROM `tblpeople`”;
$result = mysql_query($sqlQuery);
$maxrow = mysql_num_rows($result);
$page_number = ceil($maxrow/$perPage)
?>

<ul id=”pagination” >
<?php
//Load page numbers
for($i=1; $i<=$page_number; $i++)
{
echo ‘<li id=”‘.$i.'”>’.$i.'</li>’;
}
?>
</ul>
</div>

<!– jquery extension –>
<script type=”text/javascript” src=”jquery/jquery.min.js”></script>
<!– paging extension –>
<script type=”text/javascript” src=”js/paging.js”></script>

[/php]

3.Create a script for loading the data that appears in the page. Name it “loaddata.php”.

[php]

<?php
$perPage = 3;
if($_GET){
$page=$_GET[‘page’];
}

$firstPage = ($page-1)*$perPage;

$sqlQuery = “SELECT * FROM `tblpeople` limit {$firstPage},{$perPage}”;
$result = mysql_query($sqlQuery) or die(mysql_error());

while ($row = mysql_fetch_array($result)) {
echo ‘

‘;
echo ‘

‘;
echo ‘

‘;
echo ‘

‘;
echo ‘

‘;
echo ‘

‘;
}

?>

Name Address Sex Contact#
‘ .$row[‘NAME’].’ ‘. $row[‘ADDRESS’].’ ‘. $row[‘SEX’].’ ‘. $row[‘CONTACT’].’

[/php]

4.Create a  method for paging. Name it “pagination.js”.

[jquery]

if (!empty($this->line_Width)) {
$this->Write_Line();
}
$this->Wave_Image();
if ($this->img_blur && function_exists(‘imagefilter’)) {
imagefilter($this->gd_Image, IMG_FILTER_GAUSSIAN_BLUR);
}
$this->Reduce_Image();

if ($this->img_debug) {
imagestring($this->gd_Image, 1, 1, $this->img_height-8,
“$text {$fontcfg[‘font’]} “.round((microtime(true)-$ini)*1000).”ms”,
$this->GdFgColor
);
}

protected function Image_Allocate() {
// Cleaning Up
if (!empty($this->gd_Image)) {
imagedestroy($this->gd_Image);
}

$this->gd_Image = imagecreatetruecolor($this->img_width*$this->img_scale, $this->img_height*$this->img_scale);

// Background color
$this->GdBgColor = imagecolorallocate($this->gd_Image,
$this->background_Color[0],
$this->background_Color[1],
$this->background_Color[2]
);
imagefilledrectangle($this->gd_Image, 0, 0, $this->img_width*$this->img_scale, $this->img_height*$this->img_scale, $this->GdBgColor);

// Foreground color
$color           = $this->txtcolors[mt_rand(0, sizeof($this->txtcolors)-1)];
$this->GdFgColor = imagecolorallocate($this->gd_Image, $color[0], $color[1], $color[2]);

// Shadow color
if (!empty($this->txtshadowColor) && is_array($this->txtshadowColor) && sizeof($this->txtshadowColor) >= 3) {
$this->GdShadowColor = imagecolorallocate($this->gd_Image,
$this->txtshadowColor[0],
$this->txtshadowColor[1],
$this->txtshadowColor[2]
);
}
}

protected function GetCaptchaText() {
$text = $this->GetCaptchaTextDictionary();
if (!$text) {
$text = $this->GetRandomCaptchaText();
}
return $text;
}

protected function GetRandomCaptchaText($length = null) {
if (empty($length)) {
$length = rand($this->min_Word_Length, $this->max_Word_Length);
}

$words  = “abcdefghijlmnopqrstvwyz”;
$vocals = “aeiou”;

$text  = “”;
$vocal = rand(0, 1);
for ($i=0; $i<$length; $i++) {
if ($vocal) {
$text .= substr($vocals, mt_rand(0, 4), 1);
} else {
$text .= substr($words, mt_rand(0, 22), 1);
}
$vocal = !$vocal;
}
return $text;
}

function GetCaptchaTextDictionary($extended = false) {
if (empty($this->words_File)) {
return false;
}

// Full path of words file
if (substr($this->words_File, 0, 1) == ‘/’) {
$words_file = $this->words_File;
} else {
$words_file = $this->path.’/’.$this->words_File;
}

if (!file_exists($words_file)) {
return false;
}

$fp     = fopen($words_file, “r”);
$length = strlen(fgets($fp));
if (!$length) {
return false;
}
$line   = rand(1, (filesize($words_file)/$length)-2);
if (fseek($fp, $length*$line) == -1) {
return false;
}
$text = trim(fgets($fp));
fclose($fp);

protected function CleaningUp() {
imagedestroy($this->gd_Image);
}
}

[/php]

  • Second, do this following code for viewing the captcha. Name it “index.php”.

[html]

<h3>Test if you are human.</h3>
<div onload=”document.getElementById(‘strcaptcha’).focus()”>
<form action=”” method=”post”>
<img id=”imgcaptcha” src=”loadcaptcha.php”><br>
<!– Changing captcha –>
<a href=”#” onclick=”LoadCaptcha()”>Reload Captcha.</a><br>
<br>
<!– end changing –>
<input autocomplete=”off” id=”strcaptcha” name=”strcaptcha” type=
“text”><br>
<input name=”submit” type=”submit”>
</form>

[/html]

  • Third, create a script for refreshing the captcha.

[javascript]

if (isset($_POST[‘strcaptcha’])) {
if (empty($_SESSION[‘captcha’]) || trim(strtolower($_POST[‘strcaptcha’])) != $_SESSION[‘captcha’]) {
$msg = “Invalid captcha”;
$color = “background-color: #FF606C”;
} else {
$msg = “Valid captcha”;
$color = “background-color: #CCFF99”;
}

unset($_SESSION[‘captcha’]);
}

[/php]

 

For all students who need programmer for your thesis system or anyone who needs a sourcecode in any programming languages.  You can contact me @ :

 

Email – [email protected]

Mobile No. – 09305235027 – tnt

Download Sourcecode

0 thoughts on “How to Create a Captcha Image in PHP”

  1. 
    You are so cool! I don’t suppose I’ve truly read anything like this before. So wonderful to discover someone with genuine thoughts on this topic. Really.. thank you for starting this up. This website is something that’s needed on the web, someone with a bit of originality!

Leave a Comment