Today, We want to share with you captcha code in php.In this post we will show you Build Your Own CAPTCHA and Contact Form in PHP, hear for Creating custom captcha in PHP we will give you demo and example for implement.In this post, we will learn about how to implement custom captcha in php with an example.
captcha code in php
There are the Following The simple About custom captcha code in php Full Information With Example and source code.
As I will cover this Post with live Working example to develop A Simple PHP CAPTCHA Script, so the simple captcha code in php free download is used for this example is following below.
Step 1: Creating Database and Table
inquiry_form_captcha.sql
-- phpMyAdmin SQL Dump -- version 4.7.4 -- https://www.phpmyadmin.net/ -- -- Host: localhost -- Generation Time: Feb 04, 2020 at 05:15 AM -- Server version: 5.6.37 -- PHP Version: 7.1.23 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET AUTOCOMMIT = 0; START TRANSACTION; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8mb4 */; -- -- Database: `inquiry_form_captcha` -- -- -------------------------------------------------------- -- -- Table structure for table `tbl_inquiry` -- CREATE TABLE `tbl_inquiry` ( `id` int(11) NOT NULL, `user_name` varchar(255) NOT NULL, `user_email` varchar(255) NOT NULL, `subject` text NOT NULL, `Inquiry` text NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Indexes for dumped tables -- -- -- Indexes for table `tbl_inquiry` -- ALTER TABLE `tbl_inquiry` ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `tbl_inquiry` -- ALTER TABLE `tbl_inquiry` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=14; COMMIT; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Step 2: Creating Landing page
index.php
0) { $userCaptcha = filter_var($_POST["captcha_code"], FILTER_SANITIZE_STRING); $isValidCaptcha = $captcha->validateCaptcha($userCaptcha); if ($isValidCaptcha) { $userName = filter_var($_POST["userName"], FILTER_SANITIZE_STRING); $userEmail = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL); $subject = filter_var($_POST["subject"], FILTER_SANITIZE_STRING); $Inquiry = filter_var($_POST["Inquiry"], FILTER_SANITIZE_STRING); require_once "./Model/inquiry.php"; $inquiry = new inquiry(); $insertId = $inquiry->addToinquirys($userName, $userEmail, $subject, $Inquiry); if (! empty($insertId)) { $success_message = "Your message received successfully"; } } else { $error_message = "Incorrect Captcha Code"; } } ?>inquiry Form with PHP Captcha inquiry Form with PHP Captcha
Step 3: Create a captcha Image Source File
captchaImageSource.php
getCaptchaCode(6); $captcha->setSession('captcha_code', $captcha_code); $imageData = $captcha->createCaptchaImage($captcha_code); $captcha->renderCaptchaImage($imageData); ?>
Step 4: Create a PHP Captcha Model
Captcha.php
getSession("captcha_code"); if($capchaSessionData == $formData) { $isValid = true; } return $isValid; } }
Step 5: Create a PHP inquiry Model
inquiry.php
ds = new DataSource(); } function addToinquirys($userName, $userEmail, $subject, $Inquiry) { $insertQuery = "INSERT INTO tbl_inquiry (user_name, user_email, subject, Inquiry) VALUES (?, ?, ?, ?)"; $paramType = 'ssss'; $paramValue = array( $userName, $userEmail, $subject, $Inquiry ); $insertId = $this->ds->insert($insertQuery, $paramType, $paramValue); return $insertId; } }
Step 6: Create a Data Source File
lib/DataSource.php
conn = $this->getConnection(); } public function getConnection() { $conn = new \mysqli(self::HOST, self::USERNAME, self::PASSWORD, self::DATABASENAME); if (mysqli_connect_errno()) { trigger_error("Problem with connecting to database."); } $conn->set_charset("utf8"); return $conn; } public function select($query, $paramType = "", $paramArray = array()) { $stmt = $this->conn->prepare($query); if (! empty($paramType) && ! empty($paramArray)) { $this->bindQueryParams($stmt, $paramType, $paramArray); } $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $resultset[] = $row; } } if (! empty($resultset)) { return $resultset; } } public function insert($query, $paramType, $paramArray) { $stmt = $this->conn->prepare($query); $this->bindQueryParams($stmt, $paramType, $paramArray); $stmt->execute(); $insertId = $stmt->insert_id; return $insertId; } public function execute($query, $paramType = "", $paramArray = array()) { $stmt = $this->conn->prepare($query); if (! empty($paramType) && ! empty($paramArray)) { $this->bindQueryParams($stmt, $paramType, $paramArray); } $stmt->execute(); } public function bindQueryParams($stmt, $paramType, $paramArray = array()) { $paramValueReference[] = & $paramType; for ($i = 0; $i < count($paramArray); $i ++) { $paramValueReference[] = & $paramArray[$i]; } call_user_func_array(array( $stmt, 'bind_param' ), $paramValueReference); } public function getRecordCount($query, $paramType = "", $paramArray = array()) { $stmt = $this->conn->prepare($query); if (! empty($paramType) && ! empty($paramArray)) { $this->bindQueryParams($stmt, $paramType, $paramArray); } $stmt->execute(); $stmt->store_result(); $recordCount = $stmt->num_rows; return $recordCount; } }
step 7: Create a CSS File
style.css
body { font-family: arial; max-width: 610px; font-size: 0.95em; color: #232323; } .inquirey-error { color:#FF0000; font-size: 0.95em; } .inquirey-input { width: 100%; border-radius: 5px; border: #CCC 1px solid; padding: 12px; margin-top: 5px; } .inquirey-btn { padding: 12px; border-radius: 5px; background: #232323; border: #284828 1px solid; color: #FFF; width: 100%; cursor: pointer; margin-top: 4px; } .inquirey-table { border-radius: 3px; padding: 10px; border: #E0E0E0 1px solid; } .inquirey-success { margin-top: 5px; color: #478347; background: #e2ead1; padding: 10px; border-radius: 5px; } .captcha-input { background: #FFF url(./../../captchaImageSource.php) repeat-y left center; padding-left: 85px; }
I hope you get an idea about captcha code in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.