Testing for Cookies in CodeIgniter

 
 
 

Almost all the website nowadays utilise cookies in some way, not always to track but just to simply pass data from page to page or to keep a login session active. With the recent misinformation about cookies in Europe more and more web users are blocking vital cookies that are needed to run websites. This leads to confusion when the sites don't work as expected which is especially irritating when trying to login to secure areas.

I recently made this little CodeIgniter helper which will test if a user has cookies enabled. This will work nicely in CodeIgniter and integrate with a callback on form validation. It works by first setting a test cookie and on form submit (Usually on the login form) will test that the test cookie exists in our form validation.

.htaccess

Before we do anything we need to redirect users either onto or off of www (Your choice). iPhones don't work nicely with cookies set on one and read from another and could give false positives. If you are running Apache something like this should work, add a file called .htaccess in the root web directory with the following, just replace yourwebsite.com with your domain:


RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourwebsite.com
RewriteRule (.*) http://www.yourwebsite.com/$1 [R=301,L]

Helper file

Next we need to add a helper file which we can call on in our controller, just make a new file called test_cookie_helper and place it in your application helpers folder, then we just need to add the following in the file:


if(!function_exists('test_cookie')) {
    function test_cookie($test = TRUE) {
  
    //Load the CodeIgniter Instance and Cookie Helper
    $CI =& get_instance();
    $CI->load->helper('cookie');
  
    //see if we have post data and that test is true
    if($test && $_POST) {
        //If we don't have the test cookie then fail
        if(!get_cookie('test_cookie')) {
            return FALSE;
        } else {
            return TRUE;
        }
    }
  
    //Set a new test cookie
    delete_cookie('test_cookie');
    set_cookie('test_cookie', TRUE, 86400);
    return TRUE; 
 
    }
}

This function can set a test cookie by calling


test_cookie(FALSE);

or read a cookie by calling


test_cookie();

Use the helper in form validation

You can use this helper anywhere in your site with a form by firstly loading the helper


$this->load->helper('test_cookie');

into your controller method or construct, then set and test the cookie in your controller, first we will set the cookie before any validation:


//this will set the cookie on first page load
test_cookie(FALSE);

We will create a new method which we will call on within our form validation:


function _cookie_test() {
  
    //Check if the test cookie exists
    if(!test_cookie()) {
        $this->form_validation->set_message('_cookie_test', 'Cookie test failed');
        return FALSE;
    }
    return TRUE;
}

We can then load the callback in our form validation:


$this->form_validation->set_rules('password', 'password', 'required|callback__cookie_test');

Getting fancy

If you are going to use the function a lot it might be worth extending the native form_validation class so you don't need the callback method. More info on that can be found on the CodeIgniter website