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]
<div id="loader">

         &lt;?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 '

';
}

?&gt;
<table class="table " style="font-size:12px" cellspacing="0">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>Sex</th>
<th>Contact#</th>
</tr>
</thead>
<tbody>
<tr>
<td>' .$row['NAME'].'</td>
<td>'. $row['ADDRESS'].'</td>
<td>'. $row['SEX'].'</td>
<td>'. $row['CONTACT'].'</td>
</tr>
</tbody>
</table>
</div>
[/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]

&lt;h3&gt;Test if you are human.&lt;/h3&gt;
&lt;div onload="document.getElementById('strcaptcha').focus()"&gt;
&lt;form action="" method="post"&gt;
&lt;img id="imgcaptcha" src="loadcaptcha.php"&gt;&lt;br&gt;
&lt;!-- Changing captcha --&gt;
&lt;a href="#" onclick="LoadCaptcha()"&gt;Reload Captcha.&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
&lt;!-- end changing --&gt;
&lt;input autocomplete="off" id="strcaptcha" name="strcaptcha" type=
"text"&gt;&lt;br&gt;
&lt;input name="submit" type="submit"&gt;
&lt;/form&gt;

[/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

Frequently Asked Questions

How does this PHP project work?

Built with vanilla PHP (no framework) and MySQL backend. Standard structure: form HTML, PHP script handlers, MySQL via PDO or mysqli, sessions for auth, Bootstrap for responsive layout. Ready to extend for BSIT capstone scope.

What PHP and MySQL versions does this project require?

Most projects in this batch run on PHP 7.4 to PHP 8.2 with MySQL 5.7+ or MariaDB 10+. A few older projects need PHP 5.6 (deprecated, use XAMPP 7.x). To run: install XAMPP / WAMP / Laragon, extract project to htdocs, import the included .sql file via phpMyAdmin, edit the connection file (usually config.php or db_connect.php) with your DB credentials, browse to the project URL in your browser.

How do I set up the database for this PHP project?

Open phpMyAdmin (http://localhost/phpmyadmin/ on XAMPP), create a new empty database with the name specified in the project’s config.php. Click the Import tab, choose the included .sql file, click Go. Then edit config.php (or includes/connection.php) with: ‘localhost’, your MySQL username (usually ‘root’), your MySQL password (usually blank for XAMPP), and the database name.

Can I use this PHP project for a BSIT capstone or thesis?

Yes, but extend it. A bare CRUD app is too narrow for full capstone scope. Add: user roles via session checks, reports/dashboards (Chart.js + AJAX), PDF exports (TCPDF library), email notifications (PHPMailer), real domain extension (analytics, audit log, multi-branch support). Pair with Chapter 1-5 documentation matching your panel’s rubric.

Why am I getting ‘connection error’ or ‘undefined function mysqli_connect’?

Three common PHP issues: (1) Connection error: Apache + MySQL services not running in XAMPP control panel, OR database name in config.php does not match what you created in phpMyAdmin. (2) ‘undefined function mysqli_connect’: MySQL extension not enabled, in php.ini uncomment extension=mysqli (then restart Apache). (3) ‘No such file or directory’: MySQL socket path wrong, use 127.0.0.1 instead of localhost in the connection string.

Where can I find more PHP projects with source code?

Browse the PHP Projects hub for the full library (300+ vanilla PHP systems). For modern PHP MVC alternatives see Laravel Projects (74 systems) or CodeIgniter Projects (32 systems). For BSIT-focused capstone idea lists see 150 Best Capstone Project Ideas.

Related PHP Projects

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