Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Google reCaptcha Application In Php Codeigniter


reCAPTCHA is a free CAPTCHA service that helps digitize books, newspapers and radio broadcasts in the past. CAPTCHAs are usually used to avoid bots and spam attacks on a website.

Google inc. The largest internet company has made CAPTCHA services to facilitate the developers to secure / avoid spam attacks on websites built.

On this article we will applicate CAPTCHA into a PHP framework that is CodeIgniter. We only need 2 php files, create a new file named recaptcha.php in folder Application/config and write the following code:

<?php
defined('BASEPATH') OR exit('No direct script access allowed'); 
/*
* Recaptcha configuration settings
*
* recaptcha_sitekey: Recaptcha site key to use in the widget
* ‎
* recaptcha_secretkey: Recaptcha secret key which is used for communicating between your server to Google's
* ‎
* lang: Language code, if blank "en" will be used
* ‎
* recaptcha_sitekey and recaptcha_secretkey can be obtained from https://www.google.com/recaptcha/admin/
* ‎
* ‎ Language code can be obtained from https://developers.google.com/recaptcha/docs/language
* ‎
*/
$config['recaptcha_sitekey'] = "6Le89gYUAAAAAIENtNE52gZXRZbtLAOA7caL2vQY";
$config['recaptcha_secretkey'] = "6Le89gYUAAAAACwspqCQaB17kqgaUd1juP7eURwP"";
$config['lang'] = "id";

At the code on the top have 3 config is :
$config['recaptcha_sitekey'] $config['recaptcha_secretkey'] $config['lang']

To get sitekey and secretkey you can register at following link: https://www.google.com/recaptcha/admin select reCAPTCHA V2, then input your website domain. After that create a new file named Recaptcha.php in folder Application/libraries, write this following code:

<?php 
/*
* CodeIgniter NO Captcha ReCAPTCHA a.k.a reCAPTCHA Version 2.0 library
* ‎
* This library is based on official reCAPTCHA library for PHP
* ‎
* https://github.com/google/ReCAPTCHA *
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class ReCaptcha {
     private $signup_url = "https://www.google.com/recaptcha/admin";
     ‎private $_siteVerifyUrl = "https://www.google.com/recaptcha/api/siteverify?";
     ‎private $_secret, $_sitekey, $_lang;
     ‎private $_version = "php_1.0";
     ‎function __construct() {
     ‎     $this->ci = & get_instance();
     ‎     $this->ci->load->config('recaptcha', TRUE);
          ‎if ($this->ci->config->item('recaptcha_secretkey', 'recaptcha') == NULL || $this->ci->config->item('recaptcha_secretkey', 'recaptcha') == "") {
          ‎die("To use reCAPTCHA you must get an API key from <a href='" . $this->signup_url . "'>" . $this->signup_url . "</a>");
          ‎}
          ‎if ($this->ci->config->item('recaptcha_sitekey', 'recaptcha') == NULL || $this->ci->config->item('recaptcha_sitekey', 'recaptcha') == "") {
          ‎die("To use reCAPTCHA you must get an API key from <a href='" . $this->signup_url . "'>" . $this->signup_url . "</a>");
          ‎}
          ‎$this->_secret = $this->ci->config->item('recaptcha_secretkey', 'recaptcha');
          ‎$this->_sitekey = $this->ci->config->item('recaptcha_sitekey', 'recaptcha');
          ‎if ($this->ci->config->item('lang', 'recaptcha') == NULL || $this->ci->config->item('lang', 'recaptcha') == "") {
          ‎$this->_lang = 'en';
          ‎} else {
          ‎$this->_lang = $this->ci->config->item('lang', 'recaptcha');
          ‎}
}

/*
* Function to convert an array into query string
* ‎
* @param array $data Array of params
* ‎
* @return String query string of parameters
*/
private function _encodeQS($data) {
     $req = "";
     ‎foreach ($data as $key => $value) {
     ‎     $req .= $key . '=' . urlencode(stripslashes($value)) . '&';
     ‎}
     ‎return substr($req, 0, strlen($req) - 1);
}

/**
* HTTP GET to communicate with reCAPTCHA server
* ‎
* @param string $path URL to GET
* ‎
* @param array $data Array of params
*
* ‎ @return string JSON response from reCAPTCHA server
*/
private function _submitHTTPGet($path, $data) {
     $req = $this->_encodeQS($data);
     ‎$response = file_get_contents($path . $req);
     ‎return $response;
‎}

‎ /**
*
* ‎Function for rendering reCAPTCHA widget into views
* ‎
* Call this function in your view * @return string embedded HTML
*/
‎public function render() {
     $return = '<div class="g-recaptcha" data-sitekey="' . $this->_sitekey . '"></div>
     ‎<script src="https://www.google.com/recaptcha/api.js?hl=' . $this->_lang . '" async defer>
     ‎</script>';
     ‎return $return;
}

/**
* Function for verifying user's input
* ‎
* @param string $response User's input
* ‎
* @param string $remoteIp Remote IP you wish to send to reCAPTCHA, if NULL $this->input->ip_address() will be called
* @return array Array of response
*/
public function verifyResponse($response, $remoteIp = NULL) {
     if ($response == null || strlen($response) == 0) {
     ‎// Empty user's input
     ‎$return = array( 'success' => FALSE, 'error_codes' => 'missing-input' );
     ‎}
     ‎$getResponse = $this->_submitHttpGet( $this->_siteVerifyUrl, array( 'secret' => $this->_secret, 'remoteip' => (!is_null($remoteIp)) ? $remoteIp : $this->ci->input->ip_address(), 'v' => $this->_version, 'response' => $response ) );
     ‎$answers = json_decode($getResponse, TRUE);
     ‎if (trim($answers ['success']) == true) {
     ‎// Right captcha!
     ‎$return = array( 'success' => TRUE, 'error_codes' => '' );
     ‎ } else {
     ‎// Wrong captcha!
     ‎$return = array( 'success' => FALSE, 'error_codes' => $answers['error-codes'] );
     ‎‎} return $return;
‎  }
‎}

Then we will running and testing our reCAPTCHA, create a new controller named Contact.php in folder Application/controllers and the following code is below:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Contact extends CI_Controller {
    public function __construct() {
    ‎parent::__construct();
    ‎//load library
    ‎$this->load->library(array('recaptcha','form_validation'));
    ‎}
    ‎public function index() {
    ‎     $data = array( 'recaptcha_html' => $this->recaptcha->render() );
    ‎//set form validation
    ‎$this->form_validation->set_rules('username', 'Username', 'required');
    ‎$this->form_validation->set_rules('password', 'Password', 'required');
    ‎$this->form_validation->set_rules('g-recaptcha-response', '<strong>Captcha</strong>', 'callback_getResponseCaptcha');
    ‎//set message form validation
    ‎$this->form_validation->set_message('required', '{field} is required.');
    ‎$this->form_validation->set_message('callback_getResponseCaptcha', {field} {g-recaptcha-response} must filled. ');
    ‎if($this->form_validation->run() == TRUE) {
    ‎//if valid condition
    ‎}else{
    ‎$this->load->view('recaptcha');
    ‎ }
    ‎} public function getResponseCaptcha($str) {
    ‎$this->load->library('recaptcha');
    ‎$response = $this->recaptcha->verifyResponse($str);
    ‎if ($response['success']) { return true; } else {
    ‎$this->form_validation->set_message('getResponseCaptcha', '%s is required.' );
    ‎return false;
    ‎}
}
}

Create a new file named recaptcha.php in folder Application/views and write the following code:

<div class="container">
<form class="form-signin" accept="utf-8" action="<?php echo base_url() ?>contact">
<h2 class="form-signin-heading">Please sign in</h2>
‎<div class="form-group">
‎<label for="inputEmail" class="sr-only">Username</label>
‎<input type="text" name="nama" class="form-control" id="nama" placeholder="Masukkan Username Anda" value="<?php echo set_value('username') ?>" autocomplete="off">
‎<?php echo form_error('username'); ?>
‎</div>
‎<div class="form-group">
‎<label for="inputPassword" class="sr-only">Password</label>
‎<input type="password" name="nama" class="form-control" id="nama" placeholder="Masukkan Username Anda" value="<?php echo set_value('password') ?>" autocomplete="off">
‎<?php echo form_error('password'); ?>
‎</div>
‎<div class="form-group">
‎<label>Recaptcha</label>
‎<?php echo $recaptcha_html;?>
‎<?php echo form_error('g-recaptcha-response'); ?>
‎</div>
‎<div class="checkbox">
‎<label>
‎<input type="checkbox" value="remember-me"> Remember me
‎</label>
‎</div>
‎<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
‎</form>
‎</div>


Now run and test your project in localhost

Install And Setting Github Desktop At The First Time


GitHub Desktop is a desktop GUI version of Github web, the biggest and the most popular Git repository. What are the features, the following advantages and disadvantages? Check it out!

First, I'm is one of the most people not too much to use GUI for Git becoause already used Visual Studio Code which has a built in Git tool, it is enough for simple use like pull, push, commit etc. However, I also use GUI for repo Git management if needed, have tried SourceTree, Tower, and while it is using GitKraken. All of them is good, although there are have advantages and disadvantages.

And GitHub Desktop 1.0 was just released. Even as a whole, just 3 month this software is built from Beta version by Github. Somewhat late though, because the market is already using another GUI, and some even offer more complete integration  than just Github.

Download

Please download this software on the official website, https://desktop.github.com/ Unfortunately, there is only a version of Windows and Mac OS only. Although many report in the community of users, Github Desktop can run on Linux.


First Time Setting

There is a short wizard when the software is first run, standard like other Git Management GUI software. For example asked user and Git email, or Login to Github account.
Then whether to voluntarily telemetry to Github or not:

The initial appearance is really clean, there are three section menu. Create a new git repo, read an existing one or clone from Github.


Adding local repositories is also easy, just browse and Add Repository

Good interface:



There is a list of stash files that should be committed, following the history.


Advantages and disadvantages

The advantages:
- Free, 
- Native integration to Github platform,
- Net interface, 
- Simple and easy to understand
- Light

Disadvantages:
- Not too native, because using Electron as base
- Just support Github platform, others GUI can be used for Gitlab, Bitbucket etc. 
- Can't make Group Project, if managing much more repo going complicated

Insert Update Delete Using Ajax Jquery


If in a previous article I wrote about the other functions of the iframe, which I review, I use iframe as a trigger to insert, update, delete. Read: Another Function Of IFrame.
So, this time I wrote an article related to insert, update, delete methods on the webbase programming. I can say this is the correct method to insert, update, delete using jquery ajax in php programming.

Another Function of iFrame


Generally, iframe is use to display another webpage load into the main webpage. But, I use an iframe in anti-mainstream, maybe because I was too creative :D or I am too stupid in webbase programming with php and javascript. Here I use iframe as a trigger to insert, update, delete.
With connectivity to MySQL database I succeeded in changing the general function of the iframe and make it as a trigger.
In fact, is the same point to load another webpage into the main webpage. But, another webpage that loaded in this case contains queries insert, update, delete. And its iframe visibility set to hidden.

STEP-BY-STEP: AUTOMATIC BACKUP MySQL DATABASE WITH WINDOWS SCHEDULER


If you are a software developer, you may be familiar with MySQL database, and of course you also have heard about SQLyog. SQLyog is a software package that serves a purpose similar as MySQL Graphic User Interfaces (GUI) Tools.

Available Space

Pasang iklan di sini...
081235172529 (WA Only)

Available Space

Pasang iklan di sini...
081235172529 (WA Only)