Latest Post
Tampilkan postingan dengan label Captcha. Tampilkan semua postingan
Tampilkan postingan dengan label Captcha. Tampilkan semua postingan

reCaptcha style Captcha with JQuery and PHP

Written By Unknown on Senin, 07 Oktober 2013 | 08.46

This tutorial is about to creating a captcha same as recaptcha. I used CSS and PHP for this. Now you can create your own recaptcha style captcha with php and jquery. 

To read more about this article Click Here

For Demo Click Here

To download files Click Here

Formify - jQuery PHP Ajax Contact Form Templates With Captcha

Written By Unknown on Selasa, 30 Oktober 2012 | 09.27

Creating and configuring a contact form for you website sometimes become a pain if don’t have much experience dealing with commonly related issues. They become more tough if you want to include the attachment functionality as well.

Today’s freebie is a collection of open source jQuery & PHP ajax contact forms, we call it Formify. Formify is a contribution to lessen the pain of creating contact forms. The collection contains six different forms that are commonly used. These open source contact forms can be customized as well to suit the need for your website/application.

To read more about this article Click Here

For Demo Click Here

To download files Click Here

Add Customized reCaptcha in your PHP Application

Written By Unknown on Senin, 31 Januari 2011 | 11.20


Captchas are an essential part of an online web form to prevent automated spam bots accessing your application. Though creating captchas is not a rocket science in PHP and can be easily integrated into web based forms but creating a more usable and accesible captcha which can even read out captchas on the fly is a tough job.

But with reCaptcha this won’t be a big task. reCaptcha is a free captcha service that lets you integrate more accessible and friendly captchas into your web application, it shows words from various digitized books rather than creating a cryptic code generated from a random string and can read out captchas to the user.
Here’s how to integrate a customized reCaptcha into your PHP based web application.

1. Get reCaptcha API Key

First of all, you’ll need to sign up with reCaptcha for an API key to use the captchas in your application. You can get an API key fo either a single domain or if you have multiple websites, you can sign up for a global key that’ll work across domains.
After signing up, you’ll have two keys one Public an other Private.

2. Download reCaptcha Library


You don’t have to mess around with fsock functions to talk to reCaptcha service, they provide a library that’ll let you access reCaptcha service easily. Just downloa the latest reCaptcha library and place it in your application folder.

3. Add Captcha to your Form


Now to include captcha in your form, you need to include the recaptcha library you downloaded earlier into your form page and output the recaptcha widget. You’ll need your Public API key to display the captcha. Here’s the sample code that you should add to Form page, make sure it is compiled by php.

//Include anywhere within the form tag of your page
<?php
require_once('recaptchalib.php');//change path to where you placed the library
$publickey = "..."; // your public reCaptcha Key
echo recaptcha_get_html($publickey);
?>

4. Process the captcha

Now when the form is submitted, you’ll need to check the validity of the captcha and if it is ok then proceed to process rest of the form. In order to check the captcha, you’ll need the private API key and then use the recaptcha_check_answer function to check the validity.  Here’s the sample code to be include on the php page that processes the submitted form.

require_once('recaptchalib.php');
$privatekey = "...";//Your Private Key
$resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);
if (!$resp->is_valid)
        echo "<p style=\"color:red; \">The entered value doesn't match that of the shown string ".$rep->error."</p>";
else
        echo "<p style=\"color:green; font-weight:bold;\" >Form OK</p>";

With this, your captcha is ready to roll, but wait there is more to look for.

5. Customizing the reCaptcha widget

By default, the reCaptcha widget shows up in red theme. There are other themes you can use namely white, blackglass and clean. To use a different theme, add this into the head section of the page on which form is displayed.

 <script type="text/javascript">
 var RecaptchaOptions = {
    theme : '<theme name here>'
 };
 </script>

And if you’d like to have your own custom theme, you can certainly do that.

6. Creating your own Custom Theme

The captcha image and textbox in which value is to be entered is mandatory to use recaptcha, other elements like refresh, switch to audio are optional. Now in your form tag create a divelement with id as recaptcha_image and a text input box with name and id set torecaptcha_response_field.
And to avoid screen flickering while recaptcha loads, you can include all the widget elements within a div having an inline style of display:none.
Here’s the code to include within the form tag. Code is self-explanatory.

<div id="divrecaptcha" style="display:none;">

    <div id="controls"><a href="#" onclick="Recaptcha.reload(); " >Get another Captcha</a> <br />
       <a href="#" onclick="javascript:Recaptcha.switch_type('audio');" class="recaptcha_only_if_image" >Get Audio Captcha</a>
       <a href="#" onclick="Recaptcha.switch_type('image'); " class="recaptcha_only_if_audio" >Get Text Captcha</a> <br />
       <a href="#" onclick="Recaptcha.showhelp();" >Help</a>
     </div>

       <div id="recaptcha_image"></div><!--Important-->
    <p>
 <input type="text" name="recaptcha_response_field" id="recaptcha_response_field"  /><!--Important-->
<span class="recaptcha_only_if_image">Enter the words shown above separated by space</span>
<span class="recaptcha_only_if_audio">Enter the numbers you hear</span></p>

     </div>
     <?php
    require_once('recaptchalib.php');
    $publickey = "..."; // your public API key
    echo recaptcha_get_html($publickey);
    ?>

And in the head section of the form page, add this code.

<script type="text/javascript" >
 var RecaptchaOptions = {
    theme : 'custom',
    lang: 'en',
    custom_theme_widget: 'divrecaptcha' //div enclosing widget elements
 };
</script>

I added some styles to the widget elements in order to give an idea. You can apply any style to the elements you want.

<style type="text/css" >
#divrecaptcha{
    width:500px;
    font-size:12px; font-family:Arial, Helvetica, sans-serif;
}
#controls{ width:180px; float:right; }
#recaptcha_image{
    padding:2px; background:#f9f9f9;
    border:1px solid #e0e0e0;
}
#recaptcha_response_field {
   border: 1px solid #999 !important; //Text input field border color
   background-color:#ccc !important; //Text input field background color
   width:120px !important;
   padding:5px;
}
#divrecaptcha a{
     font-size:11px;    font-family:Verdana;
    text-decoration:none; color:#3366ff;
}
#divrecaptcha a:hover{
     color:113399; text-decoration:underline;
}
</style>









Create CAPTCHA Protection using PHP and AJAX

Written By Unknown on Sabtu, 13 Maret 2010 | 00.19


CAPTCHA is a simple test to determine if a user is a computer or a human. It is used to prevent spam abuse on the websites. So if you use CAPTCHA on your web site forms, this can help in stopping some bots and making life harder for other bots in accessing or using your forms.

In brief the CAPTCHA protection works by generating a random string, writing it to an image, then storing the string inside of a session or by some other method. This is then checked when the form is submitted.
The goal of this tutorial is to demonstrate how to make your own simple CAPTCHA protection using PHP and AJAX technologies.
This tutorial is very simple, but if you are unfamiliar with PHP and AJAX this is a great place to start. The tutorial consists of a HTML page for presenting a simple form that will send the data, a JavaScript file for handling the Ajax functionality, and a simple PHP page that makes the actual comparison of the what is in the text box compared to what phrase was stored in the image.
  • The AJAX HTML Page (the Front-end)
  • The JavaScript
  • The PHP Server Page (the Backend)
  • The Ways to Make It More Secure
The AJAX HTML Page (the Front-end)
The front-end of this tutorial is straight forward. We are going to create a simple HTML form with a textbox for entering the security code, dynamically generated image holding this code, a button for submitting, and a DIV that we will display the CAPTCHA test result. The following example shows how you can do that. Create a new file named captcha_test.htm, and add this code to it.
<form id="frmCaptcha" name="frmCaptcha">
<table> 
  <tr>
    <td align="left">
      <label for="captcha">Captcha</label>
    </td>
    <td>
      <input id="txtCaptcha" type="text" name="txtCaptcha" value="" maxlength="10" size="32" />
    </td>
    <td> 
      <img id="imgCaptcha" src="create_image.php" />
    </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>
      <input id="btnCaptcha" type="button" value="Captcha Test" name="btnCaptcha" 
          onclick="getParam(document.frmCaptcha)" />
    </td>
  </tr>
</table> 

<div id="result">&nbsp;</div>
</form>
After the textbox and the button we have added the result DIV. We have given it an ID so we can address it through the DOM in our JavaScript. It can easily be a SPAN or any other HTML element that can contain text. The button onclick event will start the AJAX request to the server to make the comparison.
Below is a php script that generates the CAPTCHA image and set a session that stores the security code. Create a new file named create_image.php, and add this code to it.
<?php 
//Start the session so we can store what the security code actually is
session_start();

//Send a generated image to the browser 
create_image(); 
exit(); 

function create_image() 
    //Let's generate a totally random string using md5 
    $md5_hash = md5(rand(0,999)); 
    //We don't need a 32 character long string so we trim it down to 5 
    $security_code = substr($md5_hash, 15, 5); 

    //Set the session to store the security code
    $_SESSION["security_code"] = $security_code;

    //Set the image width and height 
    $width = 100; 
    $height = 20;  

    //Create the image resource 
    $image = ImageCreate($width, $height);  

    //We are making three colors, white, black and gray 
    $white = ImageColorAllocate($image, 255, 255, 255); 
    $black = ImageColorAllocate($image, 0, 0, 0); 
    $grey = ImageColorAllocate($image, 204, 204, 204); 

    //Make the background black 
    ImageFill($image, 0, 0, $black); 

    //Add randomly generated string in white to the image
    ImageString($image, 3, 30, 3, $security_code, $white); 

    //Throw in some lines to make it a little bit harder for any bots to break 
    ImageRectangle($image,0,0,$width-1,$height-1,$grey); 
    imageline($image, 0, $height/2, $width, $height/2, $grey); 
    imageline($image, $width/2, 0, $width/2, $height, $grey); 
 
    //Tell the browser what kind of file is come in 
    header("Content-Type: image/jpeg"); 

    //Output the newly created image in jpeg format 
    ImageJpeg($image); 
    
    //Free up resources
    ImageDestroy($image); 
?>
If you view create_image.php in a browser, you should see a new JPEG image every time you refresh the page. You should see the black box with some letters and numbers on it. For more details how to create images on the fly read our tutorial Dynamic Image Generation.
The last we have to do is to include the JavaScript file that we will create on the next step.
<script language="JavaScript" type="text/javascript" src="ajax_captcha.js"></script>
The JavaScript

The JavaScript will be placed in an external file called ajax_captcha.js. Let's look into the JavaScript code.
The first function simply returns a browser specific XmlHttpRequest object. Unfortunately AJAX is supported slightly differently in IE than it is Safari, Opera and Mozilla-based browsers like Firefox. Microsoft Internet Explorer uses an Active X object while Mozilla and Safari use a native object.
//Gets the browser specific XmlHttpRequest Object 
function getXmlHttpRequestObject() {
 if (window.XMLHttpRequest) {
    return new XMLHttpRequest(); //Mozilla, Safari ...
 } else if (window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP"); //IE
 } else {
    //Display our error message
    alert("Your browser doesn't support the XmlHttpRequest object.");
 }
}
The next step we will create our XmlHttpRequest object that we can use to make the requests.
//Our XmlHttpRequest object
var receiveReq = getXmlHttpRequestObject();
After that we create a function that will make the AJAX request. This function gets two parameters: the url to send to the server and a parameter to the url with the content of the input field.
//Initiate the AJAX request
function makeRequest(url, param) {
//If our readystate is either not started or finished, initiate a new request
 if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
   //Set up the connection to captcha_test.html. True sets the request to asyncronous(default) 
   receiveReq.open("POST", url, true);
   //Set the function that will be called when the XmlHttpRequest objects state changes
   receiveReq.onreadystatechange = updatePage; 

   //Add HTTP headers to the request
   receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   receiveReq.setRequestHeader("Content-length", param.length);
   receiveReq.setRequestHeader("Connection", "close");

   //Make the request
   receiveReq.send(param);
 }   
}
At first we check if our XmlHttpRequest object is ready to make a request. If our readystate is either not started or finished then we initiate a new request. Next we open the connection to our server PHP page with the appended querystring and mark it as a "POST" request. After that we add HTTP headers to the request so that the server processes the request correctly. The next line defines the updatePage() function that will be called when we recieve the AJAX response. Finally, we actually send the request to the server.
The updatePage() function executes every time the state of the XmlHttpRequest object changes.
//Called every time our XmlHttpRequest objects state changes
function updatePage() {
 //Check if our response is ready
 if (receiveReq.readyState == 4) {
   //Set the content of the DIV element with the response text
   document.getElementById('result').innerHTML = receiveReq.responseText;
   //Get a reference to CAPTCHA image
   img = document.getElementById('imgCaptcha'); 
   //Change the image
   img.src = 'create_image.php?' + Math.random();
 }
}
At first we need to make sure our response is ready. If it is, then the content of the DIV element is filled with the response text and the CAPTCHA image is changed. One other thing you must remember you must have different names for each image. For that purpose we use the random() function.
The last function will be called every time the form is submitted. It gets the value from the text box, sets the url and passes them as a parameters to makeRequest() function.
//Called every time when form is perfomed
function getParam(theForm) {
 //Set the URL
 var url = 'captcha.php';
 //Set up the parameters of our AJAX call
 var postStr = theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value );
 //Call the function that initiate the AJAX request
 makeRequest(url, postStr);
}
That's all. If we open our page, type the security code into the text box and click the button, our DIV element will read the CAPTCHA test result without having to refresh the page.


The PHP Server Page (the Backend)

The server-side PHP file will make the actual comparison and return the result. This is very simple PHP page that only prints the result of the CAPTCHA test. You can add needed functionality yourselves. For example, if your HTML form will contain some other fields, you can send their data using the POST method and then output the data on the screen or add it into the database for further use.
<?php
//Continue the session
session_start();

//Make sure that the input come from a posted form. Otherwise quit immediately
if ($_SERVER["REQUEST_METHOD"] <> "POST") 
 die("You can only reach this page by posting from the html form");

//Check if the security code and the session value are not blank 
//and if the input text matches the stored text
if ( ($_REQUEST["txtCaptcha"] == $_SESSION["security_code"]) && 
    (!empty($_REQUEST["txtCaptcha"]) && !empty($_SESSION["security_code"])) ) {
  echo "<h1>Test successful!</h1>";
} else {
  echo "<h1>Test failed! Try again!</h1>";
}
?>
The session_start() simply continues the session. Then it's just a simple text matching which is done by the if statement. If the input text matches the stored text then the success message displayed, otherwise error message. If someone is trying to outwit you, then you should probably use a more secure way besides storing the security code in a session or a cookie that always has the same name. As an example you can store this data in MySQL database.
Create a new file named captcha.php, and add the code from the preceding example to it. And that's all for the server-side.
The Ways to Make It More Secure
You can see how it is easy to use PHP with Ajax technologies. This is very simple CAPTCHA test you can use but I'm sure you will think how to make it more powerful, robust and secure. In conclusion I just want to suggest some several things you can do to make it more secure:
  • Rotate the text randomly
  • Add random spaces in between characters
  • Use a TTF fonts and change the font randomly every time
  • Use a random text and image size every time
  • Use more advanced text distortion and colors
  • Move the lines randomly
  • Store the password in a random cookie
 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Kumpulan Kata Broadcast Blackberry - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger