Snippets Ideas

Using GeoIP in CodeIgniter application

Posted in Snippets Ideas on January 9th, 2010 by admin – 3 Comments

The company called Maxmind published free resources to use Geo localized IP services in your web applications. I remember this service is there from long time ago.

Now, we are interested in getting geo information from users in a website, so it could be a good idea to integrate GeoIP services as a CodeIgniter plugin.

@Burak posted a short but nice article at phpandstuff.com explaining how to integrate GeoIP in a PHP script.

So, based on that article, I’d integrate the GeoIP as a plugin, by following these steps:

1. Create a new plugin file under application/plugins/geo_ip.php and I put there the content that is inside geoip.inc

2. We’d need to copy the GeoIP.dat file to any location inside our application. I used resources/geo/GeoIP.dat as location.

3. Then we’d need to load the plugin file, we can do that on any particular controller file (on demand) or we can add it as autoloaded plugin in application/config/autoload.php

4. I went to the controller class where I want to use GeoIP to grab the user’s country name and code, and put this snippet:


        $gi = geoip_open('resources/geo/GeoIP.dat',GEOIP_STANDARD);
        $country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
        $this->data['country_code'] = $country_code;
        $country_name = geoip_country_name_by_addr($gi, $_SERVER['REMOTE_ADDR']);
        $this->data['country_name'] = $country_name;
        // close the database
        geoip_close($gi);

Update 2010-04-09: The code below was updated accordingly to Kubilay comment, thanks. Row 5 was switched with row 4.

That’s all, assuming that finally you will have the Country Code and Country Name under $this->data variable. You can see more info about why I use this way to pass information to view, under Using a Super Controller Class in CodeIgniter.

Related searches:

  • codeigniter geoip
  • integrate geo-ip for codeigniter
  • geoip codeigniter
  • Maxmind codeigniter
  • codeigniter maxmind
  • codeigniter geo
  • codeigniter country ip
  • php codeigniter geoip
  • maxmind api codeigniter
  • maxmind and codeigniter

Fast way to randomly choose an Array item

Posted in Snippets Ideas on September 4th, 2009 by admin – Be the first to comment

When dealing with arrays in PHP, I usually need to choose a random item to display, for example an image that rotates randomly in a homepage or interior page, actually doesn’t matter which page.

The applications and needs may differ a lot, but here is a simple snippet that you can use to randomly select an item in a PHP Array.

Basically it uses array_rand() function.

For example, imagine we have the filenames for images that we want to rotate in a page (banners or whatever). Then I’ll define an array, and then I will use array_rand() to get a random key.


$headers = array("menu_header_1.jpg", "menu_header_2.jpg", "menu_header_3.jpg", "menu_header_4.jpg");
$value = $headers[array_rand($headers)];

How to Backup files using CodeIgniter

Posted in Snippets Ideas on August 29th, 2009 by admin – 3 Comments

If you are developing a CodeIgniter application and need to backup files, let’s say in a .zip file, then you are lucky since CodeIgniter has a Zip Encoding library.

In my case, I used this library to create file backup, most of the time. But you can use it for other interesting things, for example adding string data to a .zip file and then allow users to download that file from a web page.

In this post I’ll only show the case we need to backup a directory using CodeIgniter and save the content to a .zip file.

Originally, this code read images and Flash files from the database and then save these images (binary data) into the file system. Since that action will run periodically using a Cron job, I’d like to do a backup for the existing files before overwriting. That’s where the Zip Class comes in action to compress a directory into an archive.


function _create_backup()
{
	$this->load->library('zip');
	$path = 'media/';
	$this->zip->read_dir($path);
	$result = $this->zip->archive('my_backup.zip');
	return $result;
}

If you want the zip file to have a better name, probably you can add the current date as  a suffix, so you can know where the file was created. To do that, you can replace ‘my_backup.zip’ by:


$result = $this->zip->archive('media_backup_'.date('Ymdhs').'.zip');

That will add the current date as a suffix.

Hope this snippet helps you the next time you need to backup files. If you are interested in other usages, you can refer to the CodeIgniter’s  User Guide for more information.

Related searches:

  • codeigniter backup
  • how to create backup files in codeigniter
  • backup a directory codeigniter
  • how to zip files in codeigniter
  • database backup codeigniter
  • create a site backup with codeigniter
  • codeigniter create cron job for database backup
  • backup database with codeigniter
  • backup codeigniter
  • using codeigniter zip encoding

How do I use Constants in CodeIgniter applications?

Posted in Snippets Ideas on August 28th, 2009 by admin – Be the first to comment

Usually I need to use constants or pass them as parameter in a function.

I don’t like the idea to hardcode these values in the code, nor sometimes I don’t like to use the PHP define() function to define constants since later it is a mass to search where a constant for a given entity has been defined.

Recently I started to use constants by defining these constants in a model Class (as reference you can find more info about Object Constants here).

Let’s say we have an Order_model class that manages Orders in an e-commerce application built in CodeIgniter. That Order_model class will implement the methods for create, update, list or view orders, for example.

Now, imagine we want to define simple states for each order. So, an order can be APPROVED as 1 or PENDING as 0.

A natural way to do that (wrong way IMHO) is to set the numeric value (1 or 0 in this case) hardcoded.

Instead of that, I created two object constants, as follows:


class Order_model extends Model {

    var $tablename = 'Orders';
    var $tablename_details = 'OrderDetails';

    const ORDER_STATUS_PENDING = 0;
    const ORDER_STATUS_APPROVED = 1;

Then, imagine we have an update_order_status() function in that model that will change the order status:


function update_order_status($order_id, $new_status)

Later, in the Controller class that will call this update function, I’ll use the constant in this way:


$this->order_model->update_order_status($order_id, Order_Model::ORDER_STATUS_APPROVED);

By doing that, the next time I need to change or maintain that code, I’ll go directly to the model class.

Related searches:

  • codeigniter constants
  • codeigniter define constants
  • codeigniter const
  • codeigniter define constant
  • codeigniter library constant
  • constantes en codeigniter
  • constants codeigniter
  • constantes codeigniter
  • how to use codeigniter constants
  • create an constant codeigniter

Unit Testing in CodeIgniter (Who use it?)

Posted in Snippets Ideas on August 28th, 2009 by admin – Be the first to comment

Using Unit tests is a best practice when developing software.

CodeIgniter has a Unit Testing class that is aimed to provide support for Unit Tests in your code.

Despite the CI Class for Unit Testing is quite simple, it may be very helpful to determine if what you are coding is exactly what you expect or not, by doing simple test cases.

I’d suggest to review their Unit Testing class.

The initialization of this library is quite simple simple, too. You just need to add the following line, and then the Unit Test object will be available using $this->unit.


$this->load->library('unit_test');

Once you loaded the library, you can start running simple tests. For each test, you specify the $test parameter (ie: 1+1), the expected result and the test name.

Also you can specify if a value is any of the available data types supported in the application, ie: is_string, is_bool, is_false, is_float, and so on, to evaluate if the result match with the expected data type.

Related searches:

  • codeigniter unit testing
  • simpletest codeigniter example
  • unit testing codeigniter
  • using unit testing codeigniter