How To Create Captcha With Codeigniter

Leave a Comment
In this tutorial I will show you how to use the the codeigniter captcha helper.

The CAPTCHA Helper file contains functions that assist in creating CAPTCHA images.

Frist we have load library of captcha in controller.

$this->load->helper('captcha');
Now we will create controller for the CAPTCHA so let's start.Before we create controller we will create captcha folder in root directory of project.



<?php
/**
* 
*/
class Captcha extends CI_Controller
{
 
 function __construct()
 {
  parent::__construct();
  $this->load->helper('captcha');
  $this->load->helper('url');
 }

 function index()
 {
  $vals = array(
      'img_path'  => './captcha/',
     'img_url'  => base_url().'/captcha/',
     'img_width' => 150,
     'img_height' => '50',
     'expiration' => 7200
     );

 $cap = create_captcha($vals);
 print_r($cap['word']);
 echo $cap['image'];
 }
}

?>
EXPLANATION : 1) In construct method of the controller we load captcha library for the captcha and url library for the base_url()
2) In Index method we create array of captcha.
3) Only the img_path and img_url are required.Other are optional.
4) The "captcha" folder must be writable (666, or 777).
5) The "expiration" (in seconds) signifies how long an image will remain in the captcha folder before it will be deleted. The default is two hours.
So that's it we create captcha for us.Our captcha look like this.



print_r($cap['word']); will print the word which is in captcha image we can use this word for the checking user is enter wrong value or right.

echo $cap['image']; will for the image which is shown as captcha.
If we look in captcha folder the image is store in that folder.



Helpful links


Pagination With Codeigniter

Leave a Comment
Hello Guy's,

Pagination is particularly useful when you are coding an application that interfaces with a database. A large dataset might have hundreds of possible results for one query, and pagination creates a much nicer user experience.
In this tutorial, I’ll use CodeIgniter’s pagination library to show you how you can create a paginated list of results from a MySQL database. Along the way, you’ll also see how to fix a problem with the pagination links that the library might produce.
I’ll assume you have an installation of CodeIgniter And you already ready with removing index.php from URL.
If yes then well and good otherwise you can check this tutorials How To Remove Index.php From url
let's Start Creating file in folder

After that create database test and get sql table country from Here
Let's Start with Model i.e mdl_pagination.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mdl_pagination extends CI_Model {

 function __construct(){
  parent::__construct();
 }

 function countcountry(){
  $query = $this->db->get('countries');
  return $query->num_rows();           // return total number of country
 }

 function getcountries($limit,$offset){
  $query = $this->db->get('countries',$limit,$offset);
  return $query->result_array();           // return the country 
 }

}


In Controller i.e pagination.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pagination extends CI_Controller {

 function __construct(){
  parent::__construct();
  $this->load->model('mdl_pagination');
  $this->load->library('table');
  $this->load->helper("url");
 }

 public function country($offset=null)
 {
  $this->load->library('pagination');

  $config['base_url'] = 'http://localhost/ci/pagination/country/';    // url of the page
  $config['total_rows'] = $this->mdl_pagination->countcountry(); //get total number of records 
  $config['per_page'] = 20;  // define how many records on page

  $this->pagination->initialize($config);

  $data['country'] = $this->mdl_pagination->getcountries($config['per_page'],$offset);
  $this->load->view('pagination',$data);
 }

}

In View i.e Pagination.php
<html>
<body>
 <div id="container">
  <h1>Countries</h1>
  <div id="body">
 <?php
 $this->table->set_heading('id', 'code', 'name');
 echo $this->table->generate($country);
 echo $this->pagination->create_links();
 ?>
  </div>
 </div>
</body>
</html>


Explanation: Run the page in browser with url http://localhost/ci/pagination/country
In url ci is the name of project, pagination is the name of controller after the last parameter is the name of function which is written in the controller of pagination.


When we call the country function after hitting the url in browser then first the __construct method execute from the pagination controller class it's load the model, table library, url helper and pagination library then it call the country method.In country method first we set the config of the pagination class $config['base_url'] which mean which url is called for the pagination after that how many records we have to fetch that all records we have to call the mdl_pagination which is our model class then we have set how much records we have to show in per_page.
$config['base_url'] = 'http://localhost/ci/pagination/country/';
$config['total_rows'] = $this->mdl_pagination->countcountry();
$config['per_page'] = 20;

we can also also write dynamic base_url like this
$config['base_url'] = base_url().'pagination/country/';

The base_url() function get url which is define in your config.php
$config['base_url'] = 'http://localhost/ci/';

Then we load the config or initialize config by using $this->pagination->initialize($config);. After that we fetch country data from country table using getcountries method of mdl_pagination class of model. We passed two arguments to this function $config['per_page '] as its limit then $offset as its offset of query.

First time when we load page then $config['per_page '] is 20 and $offset is null then its shows first 20 records from the table after than when we click on the next link then url change in to http://localhost/ci/pagination/country/20 then this time the value for the for the $config['per_page '] is 20 and $offset changes from null to 20. It will take 20 from the url which passed as argument in url after country method this time query will take next 20 records after leaving first 20 which is show.After that we fetch all data in $data['country'] and we pass all data in view

In View first we set heading of the table then we generate the table using echo $this->table->generate($country);.The table class which we loaded in __construct method of the controller class it calls the generate method of its by passing the $country array as argument. This generate method create the table of us with value passed as array.

That's it your pagination is ready it will look like this



we can add extra attributes in config like this
$config['next_link'] = ' >> ';
$config['prev_link'] = ' << ';
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config['full_tag_open'] = '<p>';
$config['full_tag_close'] = '</p>';
If any error occurs then leave comment after this tutorials we will discuss about that error thank you. If you want to do pagination with ajax you check here.
video tutorials

How to increase website traffics or improve alexa rank

Leave a Comment

Somedays before i read awesome blog which is about how to increase website traffic and improve alexa rank and i want say thank you to this author who give me such a great information about website traffic and improve alexa rank so i just thought share this blog with you here it is


Alexa Boostup





Nowadays webmasters are keeping their eye on Alexa ranks of their blog this may be due to the delay in the page rank update from google or due to the increasing importance of Alexa rank among guest bloggers. Today am here with an awesome trick which helps you to boost your Alexa rankings. First of all let me tell you this is a black hat method named as Alexa Autosurfing. Since it's a blackhat method I personally never used it but it seems to be a working trick. Boosting up Alexa rank is quite difficult if you are sticking to the legit ways. So check out this awesome method for Alexa boost up.



Alexa Autosurf

A Brilliant Way To Boost Alexa Rank.It works like this "1 View = 1 Point." Additional points are awarded for referring others to autosurf. Let's check out it's working of Autosurf.




How Its Works

Basically Alexa Autosurf is a traffic exchange site in which you could exchange a points in return to visitors. to gain points you should visit a blog and stay there for at least 50 seconds. Easiest way to get points in bulk is by referring the website. Refer your friends to Alexa autosurf and gain 1000 points for each refer.


Google Adsense And Autosurf

Are you planning to run autosurf on an adsense enabled blog? then you are inviting trouble to your blog. Google policies clearly says that it hate traffic exchange programs. Nowadays google is quite strict in its policies any small violations of the policies may lead to banning of the account.


Conclusion

Alexa autosurf improves your Alexa rank but it makes your blog vulnerable to adsense ban. Thinks twice before using this method of boosting up Alexa rank.


For more information Click Here

Powered by Blogger.