Tutorial ini menunjukkan bagaimana untuk melaksanakan Ajax Captcha asas dengan cepat menggunakan PHP, JQuery, dan Bootstrap. Walaupun tidak sangat selamat, ia menawarkan penyelesaian yang mudah dan disesuaikan untuk mencegah penyerahan bentuk automatik. Ia sesuai untuk situasi di mana captcha yang cepat dan mudah diperlukan tanpa kerumitan recaptcha.
Ciri -ciri Utama:
Demo:
Demo yang mempamerkan fungsi CAPTCHA tersedia (pautan ke demo akan pergi ke sini jika disediakan).
[imej demo Captcha akan pergi ke sini jika disediakan]
Muat turun:
Kod sumber lengkap boleh didapati di GitHub (pautan ke repo github akan pergi ke sini jika disediakan).
pelaksanaan:
Pelaksanaan CAPTCHA terdiri daripada komponen HTML, JQuery, dan PHP.
1. Html (menggunakan bootstrap):
<label class="" for="captcha">*Please enter the verification code shown below.</label> <div id="captcha-wrap"> <img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174028207285395.jpg" class="lazy" alt="Easy jQuery AJAX PHP Captcha - 2 minute setup " /> <img src="/static/imghw/default1.png" data-src="https://img.php.cn/" class="lazy" alt="Easy jQuery AJAX PHP Captcha - 2 minute setup " /> </div> <input class="narrow text input" id="captcha" name="captcha" type="text" placeholder="Verification Code" />
2. jQuery:
untuk pengesahan sisi pelayan. remote
$(function() { WEBAPP = { settings: {}, cache: {}, init: function() { this.cache.$form = $('#captcha-form'); // Assumes your form has id="captcha-form" this.cache.$refreshCaptcha = $('#refresh-captcha'); this.cache.$captchaImg = $('img#captcha'); this.cache.$captchaInput = $(':input[name="captcha"]'); this.eventHandlers(); this.setupValidation(); }, eventHandlers: function() { WEBAPP.cache.$refreshCaptcha.on('click', function(e) { WEBAPP.cache.$captchaImg.attr("src", "/php/newCaptcha.php?rnd=" + Math.random()); }); }, setupValidation: function() { WEBAPP.cache.$form.validate({ onkeyup: false, rules: { "captcha": { required: true, remote: { url: '/php/checkCaptcha.php', type: "post", data: { code: function() { return WEBAPP.cache.$captchaInput.val(); } } } } // Add other form field validation rules here as needed }, messages: { "captcha": { required: "Please enter the verification code.", remote: "Verification code incorrect, please try again." } // Add other form field error messages here as needed }, submitHandler: function(form) { // Handle successful form submission here (e.g., AJAX submission) } }); } }; WEBAPP.init(); });
3. Php (newCaptcha.php):
Skrip PHP ini menghasilkan imej CAPTCHA baru dan menyimpan kod dalam pemboleh ubah sesi.
<?php session_start(); $string = ''; for ($i = 0; $i < 6; $i++) { $string .= chr(rand(97, 122)); } $_SESSION['captcha'] = $string; $dir = '../fonts/'; // Path to your fonts directory $image = imagecreatetruecolor(165, 50); $font = "PlAGuEdEaTH.ttf"; // Your font file $color = imagecolorallocate($image, 113, 193, 217); $white = imagecolorallocate($image, 255, 255, 255); imagefilledrectangle($image, 0, 0, 399, 99, $white); imagettftext($image, 30, 0, 10, 40, $color, $dir . $font, $_SESSION['captcha']); header("Content-type: image/png"); imagepng($image); ?>
4. Php (checkcaptcha.php):
Skrip ini mengesahkan input CAPTCHA pengguna terhadap pemboleh ubah sesi.
<?php session_start(); if (isset($_REQUEST['code'])) { echo json_encode(strtolower($_REQUEST['code']) == strtolower($_SESSION['captcha'])); } else { echo 0; } ?>
Atas ialah kandungan terperinci Mudah jQuery ajax php captcha - persediaan 2 minit. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!