1. Copy kode berikut (kodingan untuk membuat captcha) dan simpan dalam file captcha.php
<?php session_start(); $permitted_chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ'; function generate_string($input, $strength = 10) { $input_length = strlen($input); $random_string = ''; for ($i = 0; $i < $strength; $i++) { $random_character = $input[mt_rand(0, $input_length - 1)]; $random_string .= $random_character; } return $random_string; } $image = imagecreatetruecolor(200, 50); imageantialias($image, true); $colors = []; $red = rand(125, 175); $green = rand(125, 175); $blue = rand(125, 175); for ($i = 0; $i < 5; $i++) { $colors[] = imagecolorallocate($image, $red - 20 * $i, $green - 20 * $i, $blue - 20 * $i); } imagefill($image, 0, 0, $colors[0]); for ($i = 0; $i < 10; $i++) { imagesetthickness($image, rand(2, 10)); $line_color = $colors[rand(1, 4)]; imagerectangle($image, rand(-10, 190), rand(-10, 10), rand(-10, 190), rand(40, 60), $line_color); } $black = imagecolorallocate($image, 0, 0, 0); $white = imagecolorallocate($image, 255, 255, 255); $textcolors = [$black, $white]; $fonts = [dirname(__FILE__) . '\fonts\Acme.ttf', dirname(__FILE__) . '\fonts\Ubuntu.ttf', dirname(__FILE__) . '\fonts\Merriweather.ttf', dirname(__FILE__) . '\fonts\PlayfairDisplay.ttf']; $string_length = 6; $captcha_string = generate_string($permitted_chars, $string_length); $_SESSION['captcha_text'] = $captcha_string; for ($i = 0; $i < $string_length; $i++) { $letter_space = 170 / $string_length; $initial = 15; imagettftext($image, 24, rand(-15, 15), $initial + $i * $letter_space, rand(25, 45), $textcolors[rand(0, 1)], $fonts[array_rand($fonts)], $captcha_string[$i]); } header('Content-type: image/png'); imagepng($image); imagedestroy($image);
2. Ini kodingan untuk di form HTML (silakan customize sesuai kebutuhan)
<form action="" method="post" id="imeiForm"> <label>Cari :</label> <input type="text" name="keyword" id="keyword"> <img src="captcha.php" alt="CAPTCHA" class="captcha-image"><i class="fas fa-redo refresh-captcha"></i> <input type="text" id="captcha" name="captcha_challenge" pattern="[A-Z]{6}"> </div> <button type="submit" name="cari">Cari</button> </form>
3. Ini kodingan untuk action ketika form diinput
if ($_POST) { $keyword = ""; if (isset($_POST['captcha_challenge']) && $_POST['captcha_challenge'] == $_SESSION['captcha_text']) { // INI ACTION KETIKA CAPTCHA BENAR if (isset($_POST["cari"])) { $imei = cari($_POST["keyword"]); if ($imei[0]['imei'] != NULL) { $imei = 'Imei Terdaftar di database Kemenprin.'; } else { $imei = "Imei Tidak di database Kemenprin."; } echo "<script>document.getElementById('keyword').value = '" . $_POST["keyword"] . "'</script>"; echo $imei; } } else { echo "<script>document.getElementById('keyword').value = '" . $_POST["keyword"] . "'</script>"; echo '<p>You entered an incorrect Captcha.</p>'; } }
4. Jangan lupa tambahkan session
session_start();
5. Juga tambahkan refresh button di captcha
<script> var refreshButton = document.querySelector(".refresh-captcha"); refreshButton.onclick = function() { document.querySelector(".captcha-image").src = 'captcha.php?' + Date.now(); } </script>
6. Juga tambahkan file-file font yang dibutuhkan didalam folder fonts.
7. Selesai.
Copyright © 2022. MFJRID.