Home » Php » PHP captcha class is not displaying the image – PHP

PHP captcha class is not displaying the image – PHP

Posted by: admin February 22, 2020 Leave a comment

Q(Question):

I just read a few tutorials about CAPTCHA from the net..I got the class captcha.php and a main file ..I pasted a few codes from the net but I cannot run the CAPTCHA as expected.The image is not being diaplayed….Instead I am getting the value from the alt in the image tag.

Here are my codes for the main file :

<?php session_start() ?>
<html>
<body>
<form method="post" action="">
<table bgcolor="#CCCCCC">
<tr><td><input type="text" name="validator" id="validator" size="4" />
<img src="captcha.php" alt="CAPTCHA image" align="top" /></td></tr>
<tr><th align="center"><input type="submit" value="Submit"></th></tr>
</table>
</form>
<?php
if(isset($_POST["captcha"]))
if (!empty($_POST['validator']) && $_POST['validator'] == $_SESSION['rand_code']) {
echo "Validated";
unset($_SESSION['rand_code']);
}
?>
</body>
</html>

I tested the source of the image in the same directory as the captha.php and it works fine.

I will be highly obliged if anyone can help me in this ..Thanks

A(Answer):

Sounds like your captcha.php isn’t working as expected. However, without more information (maybe the class code?) we cannot help any further.

A(Answer):

my captcha.php is like the following :

<?php
session_start();
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
function _generateRandom($length=6)
{
$_rand_src = array(
array(48,57) //digits
, array(97,122) //lowercase chars
// , array(65,90) //uppercase chars
);
srand ((double) microtime() * 1000000);
$random_string = "";
for($i=0;$i<$length;$i++){
$i1=rand(0,sizeof($_rand_src)-1);
$random_string .= chr(rand($_rand_src[$i1][0],$_rand_src[$i1][1]));
}
return $random_string;
}
$im = @imagecreatefromjpeg("captcha.jpg");
$rand = _generateRandom(3);
$_SESSION['captcha'] = $rand;
ImageString($im, 5, 2, 2, $rand[0]." ".$rand[1]." ".$rand[2]." ", ImageColorAllocate ($im, 0, 0, 0));
$rand = _generateRandom(3);
ImageString($im, 5, 2, 2, " ".$rand[0]." ".$rand[1]." ".$rand[2], ImageColorAllocate ($im, 255, 0, 0));
Header ('Content-type: image/jpeg');
imagejpeg($im,NULL,100);
ImageDestroy($im);
?>

A(Answer):

Hey.

I would guess it has something to do with the imagecreatefrompng function. Try verifying that the image it creates is valid. (Something you should always try to do.)

header('Content-type: image/jpeg');
$im = @imagecreatefromjpeg("captcha.jpg");
if($im) {
// Display the CAPTCHA thing
}
else {
$_SESSION['captcha'] = false;
readfile('captcha-error.jpg');
}

Now it will display the captcha-error.jpg image when a real captcha image can not be created. – Could be a error message, or it could even be a standard, temporary captcha that is displayed until you can fix the problem.

P.S.
You may want to check out reCAPTCHA.

A(Answer):

hi brother. i have implemented what you suggested to do. By doing selector (if else) operation to get the error. But fortunately my imagecreate() function work fine.
Also i have called the captcha to another (tester) file to test the program with have the same programs codes with my main program. Captcha that ran in my tester file programs work perfectly but captcha that ran in my main program doesn’t work at all.

my captcha_isi.php (my captcha) is:


<?php
// START CAPTCHA
session_start();
header("Content-type: image/png");
// inisialisasi bilangan acak untuk captha
$_SESSION["captcha"] = "";
//membuat gambar dengan format png
$img = imagecreate(120, 50); // 200, 50
// An Improvitation
if ($img) {
imagecolorallocate($img, 69, 179, 157);
$font = "Allura-Regular.otf";
$fontColor = imagecolorallocate($img, 253, 252, 252);
$ukuranFont = 15;
$posisiFont = rand(20, 40);
for ($i=0; $i <=5 ; $i++) { // 5 menentukan jumlah bilangan captcha. 5 berarti 6 digit
//$angka = rand(1111, 9999);
$angka = rand(0, 9);
$_SESSION["captcha"] .= $angka;
$kemiringan =rand(10, 40); // 20, 20
imagettftext($img, $ukuranFont, $kemiringan, 8 + 15 * $i, $posisiFont, $fontColor, $font, $angka);
}
//menciptakan gambarnya
imagepng($img);
imagedestroy($img);
} else {
$_SESSION["captcha"] = false;
echo("<script>");
echo("alert('Error Captcha !');");
echo("</script>");
}
// END OF CAPTCHA
?>

my file where the captcha is used and then will be called in to either my tester program file or my main program file :


<script type="text/javascript">
$(document).ready(function(){
$("#expand").click(function(){
$("#bodyfilter").slideToggle(500);
});
});
function jadiKeterangan(){
$("#jadiKeterangan").html("Keterangan"); // untuk mengubah text pada header kolom status surat
}
</script>
<?php
//session_start();
// MENAMPILKAN ALERT SALAH NOREG
if (isset($_GET["noregx"])) {
if ($_GET["noregx"] == "salah") {
$salahNoreg = "<div class='alert'>Nomor Registrasi Atau Nomor Surat Masuk Tidak Terdaftar !</div>";
}
}
?>
<form name="frm" action="?mod=<?php echo($_GET["mod"]); ?>" method="post">
<div class="panelcontainer" style="width: 100%;">
<h3><?php echo $salahNoreg; ?><div style="display: block; float: left;"><div style="clear: both;"></div>FILTER DATA PENCARIAN</div>
<input type="button" value="+" style="float: right; display: block; font-weight: bold; width:40px;" id="expand" /><div style="clear: both;"></div></h3>
<div class="bodypanel" id="bodyfilter">
<table border="0px" cellspacing='0' cellpadding='0' width='100%'>
<tr>
<td width='20%'>Nomor Register</td>
<td width='10px'>:</td>
<td><input type="text" name="noreg_sm" value="<?php echo($_POST["noreg_sm"]); ?>" required="required"/></td>
</tr>
<tr>
<td></td>
<td></td>
<td><p style="color:red; font-size:10px;">* Nomor Register Wajib Diisi</p></td>
</tr>
<tr>
<td width='20%'>Nomor Surat Masuk</td>
<td width='10px'>:</td>
<td><input type="text" name="no_surat_sm" value="<?php echo($_POST["no_surat_sm"]); ?>" required="required"/></td>
</tr>
<tr>
<td></td>
<td></td>
<td><p style="color:red; font-size:10px;">* Nomor Surat Masuk Wajib Diisi</p></td>
</tr>
<tr>
<td width='20%'><img src="captcha_isi.php" alt="gambar" title="Captcha"></td>
<!--<td width='20%'><img src="<?php //echo("captcha_isi.php");?>" alt="gambar"></td>-->
<td width='10px'>:</td>
<td><input type="text" maxlength="6" name="captcha_track" class="captcha" value="" required="required"/></td>
</tr>
<tr>
<td></td>
<td></td>
<td><p style="color:red; font-size:10px;">* Captcha Wajib Diisi</p></td>
</tr>
</table>
<div class="kelang"></div>
<table border="0px" cellspacing='0' cellpadding='0' width='40%'>
<tr>
<td width='33%'><input type="submit" name="submit" value='Filter' style="width: 100%;" /></td>
<td width='33%'><input type="reset" value='Reset' style="width: 100%;" /></td>
<!--<td width='33%'><button style="width: 100%;" onclick="document.location.href='?mod='"/>Kembali</button></td>-->
<td width='33%'><input type="button" class="btnkembali" value="Kembali" style="width: 100%;" onclick="window.location.href='?mod='"/></td>
</tr>
</table>
</div>
</div>
</form>
<div class="kelang"></div>
<div class="panelcontainer" style="width: 100%;">
<h3>DAFTAR SURAT KELUAR YANG DITERIMA</h3>
<div class="bodypanel table-responsive" style="padding: 2px;">
<table border="0px" cellspacing='0' cellpadding='0' width='100%' class="listingtable">
<thead>
<tr class="headertable">
<th>No.</th>
<th>No. Registrasi</th>
<th>No. Surat Masuk</th>
<th>Tgl Surat Masuk</th>
<th>Tgl Surat Diterima</th>
<th>Perihal Surat Masuk</th>
<th>Skpd Pengirim</th>
<!--<th>Status Surat Masuk</th>-->
<th>Status Surat Balasan</th>
<th id="jadiKeterangan">Posisi Surat Balasan</th>
</tr>
</thead>
<tbody>
<?php
// CHECKING IS THE INPUT ARE ALREADY FILLED ?
// CAPTCHA VALIDATOR
//session_start();
// END CAPTCHA
if (isset($_POST["submit"]) && ($_POST["noreg_sm"] != "" && $_POST["no_surat_sm"] != "" && ($_SESSION["captcha"] == $_POST["captcha_track"]))) {
//WHILE LOOP STARTED
$no = 1;
while ($data = mysql_fetch_assoc($sqlQuery2)) {
?>
<tr>
<td align="center"><?php echo $no++;?></td>
<td align="center"><?php echo $data["noreg_sm"]; ?></td>
<td align="center"><?php echo $data["no_surat_sm"];?></td>
<td align="center"><?php echo $data["tgl_sm"];?></td>
<td align="center"><?php echo $data["tgl_terima_sm"];?></td>
<td align="center"><?php echo $data["perihal_sm"];?></td>
<td align="center"><?php echo $data["skpd_pengirim"];?></td>
<td align="center"><?php echo konversiStatusSK($data["status_sk"]);?></td>
<?php
if ($data["status_sk"] > 2) {
?>
<script>jadiKeterangan();</script>
<td align="center"><?php echo $data["no_surat_sk"]; ?></td>
<?php
}else{
?>
<td align="center"><?php echo $data["lvl_tujuan_sk"];?></td>
<?php
}// END OF ELSE IF FOR CHECKING STATUS SK
?>
</tr>
<?php
} // END OF WHILE LOOP
} // END OF IF CHECK INPUT FILLED IN UP OR NOT YET
?>
</tbody>
</table>
</div>
</div>

my tester file is :


<?php
error_reporting(0);
ob_start(); ?>
<?php
session_start();
include("../../php/koneksi.php");
include("../../php/fungsi.php");
date_default_timezone_set("Asia/Jakarta");
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>:: TATA NASKAH DINAS ELEKTRONIK BADAN KEPEGAWAIAN DAERAH DAN PENGEMBANGAN SDM PEMERINTAH KOTA MEDAN ::</title>
<link rel="shortcut icon" href="image/favicon no sharpen.ico" />
<script type="text/javascript" src="../../js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="../../js/jquery.alphanum-master/jquery.alphanum.js"></script>
<script type="text/javascript" src="../../js/jquery-ui-1.8.19.custom.min.js"></script>
<script src="../../js/jquery.alerts.js"></script>
<script type="text/javascript" src="../../js/jquery.dataTables.js"></script>
<script type="text/javascript" src="../../ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="../../ckfinder/ckfinder.js"></script>
<link rel="stylesheet" href="../../js/alert/jquery.alerts.css" />
<link rel="stylesheet" href="../../css/jquery-ui-1.10.3.custom.min.css" />
<link rel="stylesheet" href="../../css/demo_table_jui.css" type="text/css" media="all" />
<link rel="stylesheet" href="../../css/customv1.css" type="text/css" media="all" />
<link rel="stylesheet" href="../../custom/css/menubar.css" type="text/css" />
<link href="../../bootstrap/css/bootstrap.css" rel="stylesheet" />
<link href="../../bootstrap/css/bootstrap-theme.min.css" rel="stylesheet" />
<script src="../../bootstrap/js/bootstrap.bundle.js"></script>
<script src="../../bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="../../bootstrap/js/bootstrap.js"></script>
<script src="../../bootstrap/js/bootstrap.min.js"></script>
<link href='https://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="custom/login/bootstrap.min.css" type="text/css" /><!--CHECKED-->
<script src="../../custom/login/bootstrap.min.js"></script>
</head>
<body role="document" id="web_body">
<div id="main-wrapper">
<div id="header"><?php include("../widget/header_new.php"); ?></div>
<div class="line"></div>
<div id="menubar">
</div>
<div id="running_text" style=''>
<?php
// include("isi/widget/running_text.php"); //FILE TIDAK ADA
?>
</div>
<div id="main-body">
<?php include("tracking_surat.php"); ?> <!-- WHERE THE CAPTCHA IS USED -->
</div>
<div style="clear: both;"></div>
</div>
<div id="main-bottom">
<div id="footer"><?php include("isi/widget/footer.php"); ?></div>
<script src="js/includes/jsHandler.js"></script>
<script>
window.onload = function(){ return start('<?php echo basename($_SERVER['REQUEST_URI'], '?' . $_SERVER['QUERY_STRING']);?>'); };
window.onbeforeunload = function(){ return end(); };
window.onclose = function(){ return end(); };
</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-87083023-1', 'auto');
ga('send', 'pageview');
</script>
</div>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-87083023-1', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>
<?php ob_end_flush(); ?>

this is my main programs :


<?php
//error_reporting(E_ALL & ~E_NOTICE); // edited on 10-09-2019 E_ALL & ~E_NOTICE means reporting all errors except notice
//
error_reporting(0);
ob_start(); ?>
<!--<!?php
register_shutdown_function(function(){
$err = error_get_last();
if($err != null){
var_dump($err);
}
});
?>-->
<?php
session_start();
include("php/koneksi.php");
include("php/fungsi.php");
date_default_timezone_set("Asia/Jakarta");
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>:: TATA NASKAH DINAS ELEKTRONIK BADAN KEPEGAWAIAN DAERAH DAN PENGEMBANGAN SDM PEMERINTAH KOTA MEDAN ::</title>
<link rel="shortcut icon" href="image/favicon no sharpen.ico" />
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="js/jquery.alphanum-master/jquery.alphanum.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.19.custom.min.js"></script>
<script src="js/jquery.alerts.js"></script>
<script type="text/javascript" src="js/jquery.dataTables.js"></script>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="ckfinder/ckfinder.js"></script>
<link rel="stylesheet" href="js/alert/jquery.alerts.css" />
<link rel="stylesheet" href="css/jquery-ui-1.10.3.custom.min.css" />
<link rel="stylesheet" href="css/demo_table_jui.css" type="text/css" media="all" />
<link rel="stylesheet" href="css/customv1.css" type="text/css" media="all" />
<link rel="stylesheet" href="custom/css/menubar.css" type="text/css" />
<link href="bootstrap/css/bootstrap.css" rel="stylesheet" />
<link href="bootstrap/css/bootstrap-theme.min.css" rel="stylesheet" />
<script src="bootstrap/js/bootstrap.bundle.js"></script>
<script src="bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="bootstrap/js/bootstrap.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<link href='https://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="custom/login/bootstrap.min.css" type="text/css" /><!--CHECKED-->
<script src="custom/login/bootstrap.min.js"></script>
</head>
<body role="document" id="web_body">
<div id="main-wrapper">
<div id="header"><?php include("isi/widget/header_new.php"); ?></div>
<div class="line"></div>
<div id="menubar">
<?php
?>
</div>
<div id="running_text" style=''>
<?php
// include("isi/widget/running_text.php"); //FILE TIDAK ADA
?>
</div>
<div id="main-body">
<?php
if(!isset($_SESSION["id_pengguna"]) && $_GET["mod"] <> "daftar_pengguna" && $_GET["mod"] <> "tracking_surat"){
include("isi/panel/login_new.php");
}elseif(!isset($_SESSION["id_pengguna"]) && $_GET["mod"] == "tracking_surat"){
//TRACKER
include("isi/panel/tracking_surat.php"); // WHERE THE CAPTCHA IS USED
}else if(!isset($_SESSION["id_pengguna"]) && $_GET["mod"] == "daftar_pengguna"){
include("isi/panel/daftar_pengguna.php");
}else{
switch($_GET["mod"]){
case "test" :
include("test.html");
break;
case "info" :
include("isi/widget/info.php");
break;
case "input_tanggal_libur":
include("isi/panel/input_tanggal_libur_nasional.php");
break;
}
?>
</div>
<div style="clear: both;"></div>
</div>
<div id="main-bottom">
<div id="footer"><?php include("isi/widget/footer.php"); ?></div>
<script src="js/includes/jsHandler.js"></script>
<script>
window.onload = function(){ return start('<?php echo basename($_SERVER['REQUEST_URI'], '?' . $_SERVER['QUERY_STRING']);?>'); };
window.onbeforeunload = function(){ return end(); };
window.onclose = function(){ return end(); };
</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-87083023-1', 'auto');
ga('send', 'pageview');
</script>
</div>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-87083023-1', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>
<?php ob_end_flush(); ?>

A(Answer):

Please help me to solve this. i have tried everything that i can do. but the main program file still not showing the captcha.
I would appreciate for any of your help.
Thank you !