Recommend this page to a friend! |
Classes of sameer shelavale | PHP Multi CAPTCHA | README.md | Download |
|
Downloadmulti-captchaAs the name says, this is one package to render them all! Multiple types of CAPTCHA in one package. Sometimes we just need to avoid spambots still want to keep the forms user-friendly and sometimes we need complete protection even if becomes a bit complex for users. MultiCaptcha helps you have uniform Captcha validation code over your projects. Key Features
Supported types of CAPTCHACaptcha Type | Screenshot | Description | Protection | Difficulty for humans ------------- | ---------- | ----------- | -------------- | -------------- image | | Asks user to identify all or some characters from the jpg image. Live demo | High | Average gif | | Asks user to identify all or some characters from the animated image. Live demo | High | Average ascii | | Asks user to identify all or some characters from code displayed in ASCII art. Live demo | Moderate | Average math | | Asks user to solve simple mathematical expression. Live demo | Below Average | Easy honeypot | | Asks user to leave the captcha field blank. Live demo | Low | Very Easy recaptcha | (image is resized) | Google ReCaptcha. Asks user to identify all characters displayed in the jpg image. Live demo | High | Average nocaptcha | (image is resized) | Google NoCaptcha(Recaptcha v2.0). Live demo | Moderate | Average Installation:Using ComposerCommand LineYou can install MultiCaptcha using Composer by doing
composer.jsonAlternatively, you can add it directly in your composer.json file in require block
and then run
PHP IncludeOr you can download the zip/rar archive and extract it and copy the /src folder to appropriate location in your project folder. And then include the captcha.php Note: Make sure the cache directory is writable.
Usage:Initialize:The minimal code required to initialize the captcha looks like:
The above code will initialize the captcha object to output "image" captcha which is default. Note: Its important to set your own secret key, as its used to encrypt and decrypt the captcha fields. And more customized code looks like:
Now, that we have basic knowledge of how to initialize it, lets look at the supported parameters in details, Supported Parametersparam | description ----- | ----------- secret | Your secret code for encryption and decryption of the form. It is recommended that you use different codes for each different web form. life | total number of hours the generated captcha will be valid. If you set it to 2, then after 2 hours the validate() function will return false even if you enter the correct code. Basically it means the user is expected to submit the form within these many hours after opening the form. customFieldName | a custom name for the captcha answer field. If not provided it will use encrypted random name for the field. Note: Recaptcha type does not honor this parameter. options | field contains the array with type of captcha/s that can be rendered as keys and their configurations as value array. If we pass more than one captcha type with it's configuration, it will randomly display one type of captcha from the supplied types. We will see configuration details of each type in details in next section. refreshUrl | (Optional) url from which we will GET the new captcha using AJAX. If not provided refresh button will not be displayed. Also note that this feature is useful mainly with image, gif, ascii and math captcha. helpUrl | (Optional) url which will provide help related to the captcha type. This url will open in new tab/window. OptionsRight now we can render 7 types of captcha namely image, gif, ascii, math, honeypot, recaptcha and nocaptcha. Now lets look in details at the supported configuration parameters for each of them. Image Captcha| Configuration Param | Default Value | Required | Description | | ------------------- | ------------- | -------- | ---------- | | minCodeLength | 4 | Optional | Maximum length of code to be displayed in the image | | maxCodeLength | 8 | Optional | Maximum length of code to be displayed in the image | | maxRequired | 5 | Optional | Maximum number of characters it can ask the user to identify | | minRequired | 3 | Optional | Minimum number of characters it can ask the user to identify | | noiseLevel | 25 | Optional | Number of background noisy characters to be added as noise | | width | 150 | Optional | Width of the captcha image in pixels | | height | 40 | Optional | Height of the captcha image in pixels | | font | src/fonts/segoesc.ttf | Optional | Path to the font file which will be used for creating the characters | Gif Captcha(GIF Animated captcha)| Configuration Param | Default Value | Required | Description | | ------------------- | ------------- | -------- | ---------- | | totalFrames | 60 | Optional | Total number of frames to be produced. Please note having too many frames may overload server during heavy traffic | | delay | 5 | Optional | delay between frames in Millisecond | minCodeLength | 4 | Optional | Maximum length of code to be displayed in the image | | maxCodeLength | 8 | Optional | Maximum length of code to be displayed in the image | | maxRequired | 5 | Optional | Maximum number of characters it can ask the user to identify | | minRequired | 3 | Optional | Minimum number of characters it can ask the user to identify | | noiseLevel | 25 | Optional | Number of background noisy characters to be added as noise | | width | 150 | Optional | Width of the captcha image in pixels | | height | 40 | Optional | Height of the captcha image in pixels | | font | src/fonts/comic.ttf | Optional | Path to the font file which will be used for creating the characters | ASCII Captcha| Configuration Param | Default Value | Required | Description | | ------------------- | ------------- | -------- | ---------- | | fonts | comic.ttf | Optional | array containing font name(without extension) as key and the size to be rendered in pixels as value. The figlet fonts vary in size when rendered so in order to control the height and width of the captcha we need to pass the font-size. For large fonts you can pass smaller value of font size. | | fontPath | null | Optional | If you want your own Figlet fonts you can specify path to the folder containing them here. | | minCodeLength | 4 | Optional | Maximum length of code to be displayed in the image | | maxCodeLength | 8 | Optional | Maximum length of code to be displayed in the image | | maxRequired | 5 | Optional | Maximum number of characters it can ask the user to identify | | minRequired | 3 | Optional | Minimum number of characters it can ask the user to identify | ASCII font names: You can find the currently supported ASCII figlet fonts in src/types/ascii/fonts/ folder and use the name of the font without the .flf extension. Example of ASCII captcha configuration:
Note, the fonts parameter, it has the font name without extension as key. and the size in pixels as the value. Unless you provide a fontPath parameter, it will look in src/types/ascii/fonts/ folder for that font. Math Captcha(Simple Mathematical expression)| Configuration Param | Default Value | Required | Description | | ------------------- | ------------- | -------- | ---------- | | level | 4 | Optional | Number of variables(digits) in the mathematical expression | | description | null | Required | Some text asking the user to solve the mathematical expression. | Honeypot Captcha| Configuration Param | Default Value | Required | Description | | ------------------- | ------------- | -------- | ---------- | | description | null | Required | Some text asking the user to leave the captcha field blank(bots will try to fill it up and get caught) | Recaptcha| Configuration Param | Default Value | Required | Description | | ------------------- | ------------- | -------- | ---------- | | publicKey | null | Required | You Recaptcha Public key given to you by Google | | privateKey | null | Required | You Recaptcha Public key given to you by Google | Note: You can register and get your recaptcha keys at http://www.google.com/recaptcha Nocaptcha| Configuration Param | Default Value | Required | Description | | ------------------- | ------------- | -------- | ---------- | | siteKey | null | Required | You Recaptcha Site key given to you by Google | | secretKey | null | Required | You Recaptcha Secret key given to you by Google | | lang | en | Optional | language code as on https://developers.google.com/recaptcha/docs/language | Note: You can register and get your recaptcha keys at http://www.google.com/recaptcha You can generate random type of captcha from multiple configured types.For example You can do:
And then it will generate a random type of captcha from the 4 types which are configured RenderYou can use the following one liner to render the captcha
That will do it.(note that $captcha is the name of object you initialized) RefreshTo display the refresh button, its necessary to provide the refreshUrl. Then in script of that url you can do.
Note: make sure no html is displayed before or after the ValidateYou can validate your form data simply by doing
Note: We need to pass all the submitted data to the validate function because the name of captcha field is encrypted & random. If you specify the customFieldName parameter it will require that field and the challenge field for validation. E.g. if customFieldName = my_captcha, then you need to pass an array
or
Themes/customizationMultiCaptcha ships with a default theme named DefaultTheme. This theme supports customization of its color/style through various parameters nested under themeOptions. e.g. you can change the background color to blue
Note: each captcha type can have its own theme and themeOptions themeOptions for DefaultTheme| Configuration Param | Description | Default Value | | ------------------- | ----------- | ------------- | | fieldClass | class for the input field | | fieldStyle | css styles for the input field | background-color:#f66a03; border:2px solid #fff; font-size:120%; font-weight:bold; border-radius:3px;width:144px; | containerStyle | css styles for the main container which contains all captcha elements (without the style= part and without enclosing quotes) | border:3px solid #000; border-radius: 5px; padding: 10px; display: table; margin: 2px; background-color: #f69d03; font-family: Arial; font-size: 14px; max-width: 180px;position:relative | questionImageStyle| css styles for the image code(for image or gif types ) | border-radius:3px; margin-bottom:5px; | questionTextStyle | css styles for the question/challenge text(for math type)| font-size:120%; font-weight:bold;background-color:#ccc; border-radius:3px; padding:4px;margin-bottom:2px;text-align:center;display:block;min-width:172px; | questionAsciiStyle| css styles for ASCII text(for the challenge text of ascii type) | background-color:#ccc; border-radius:3px; padding:4px;margin-bottom:2px;text-align:center;display:block;min-width:172px; | questionContainerStyle | css styles for the container of the question/challenge | none ; | labelStyle | css style for label | font-size:80%; line-height:100%; | helpBtnClass | css class for the help butto | btn-help | | helpBtnText | text to show on the help button | ? | refreshBtnClass | css class for the refresh button | btn-refresh | refreshBtnText | text to show up on the refresh button | ↺ | extraHtml | extra css styles(with the enclosing style tag) | <style type="text/css"> a.btn-refresh, a.btn-help{ background-color:#fff; text-decoration:none; color:#f66a03; padding:1px 2px; border-radius:2px; vertical-align:top; margin-left:2px; display:inline-block; width:12px; height:12px; text-align:center; line-height:100%; font-size:12px; } </style> With the above themeOptions you will be able to change the look and feel of the default theme. However if you need to change the placements of the elements you will have to write your own theme by extending the DefaultTheme Please refer example/theming.php for working example of themeOptions. Extending the DefaultTheme
CacheMulticaptcha uses file cache to record the answered captcha and uses it to block brute-force attack using single captcha and multiple answers. It stores the unique id of captcha and expiration time in the record, and that record is kept till the captcha expires. It uses file cache to store these records. To avoid the cache becoming too big the records are dispersed over multiple files. The number of files to be used for caching is specified by a variable $cacheSize and the directory in which the files should be stored is specified by $cacheDir, you can pass both of these variables as params to the constructor. Default cache size is 10, but you can increase it for busy websites. Note: The cache directory MUST be writable if you are using the default file cache implementation. Features under progress
Planned Features
LicenseAGPL3.0, Free for non-commercial use. Email me at samiirds@gmail.com for other type of licensing. |