Tutorialsplane

Codeigniter Input Class Library


Codeigniter Input Class Library -This library contains various functions that are used to pre-process global input data for security and fetching input data. Input class library is auto-generated, no need to load this library. This class is initialized automatically to the system so no need to do it manually. Here in this tutorial, we are going to explain how to use input class library.


Codeigniter input class library

Let us understand how input class library works in codeigniter with examples.

Class reference:-

There are following references available in input class library. Now we will explain one by one.

1. Raw input stream.

This function is used to only read property of data.

$raw_input_stream()

2. Post.

This is used to post value of field.

post([$index = NULL[$xss_clean = NULL]])

EXAMPLE

Here is simple demo of post class.

$this->input->post('field_data');
$this->input->post('field_data', TRUE);
$this->input->post(NULL, TRUE);
$this->input->post(NULL, FALSE);
$this->input->post(array('field1', 'field2'), TRUE);

3. Get.

This is used to fetch data in get method only.

get([$index = NULL[$xss_clean = NULL]])

EXAMPLE

Here is simple demo of get class.

$this->input->get('field_data', TRUE);
$this->input->get(NULL, TRUE);
$this->input->get(NULL, FALSE);
$this->input->get(array('field1', 'field2'));

4. Post get.

This method works same as post and get method combined. It will search both stream but looking in POST first, and then in GET.

post_get($index[$xss_clean = NULL])

EXAMPLE

Here is simple demo of post get class.

$this->input->post_get('field_data', TRUE);

5. Get post.

This method works same as previous method. It will search both stream but looking in GET first, and then in POST.

get_post($index[, $xss_clean = NULL])

EXAMPLE

Here is simple demo of GET/POST class.

$this->input->get_post('field_data', TRUE);

6. Cookies.

This method is used to fetch the cookies data and display it.

cookie([$index = NULL[$xss_clean = NULL]])

EXAMPLE

Here is simple example of cookies class.

Controller code:-

Your controller code now look like this:-

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class input_controller extends CI_Controller 
{
public function input()
{
set_cookie('cook','Hello user','3600');
echo get_cookie('cook');
}}
??>

Output will be like this.

7. Server.

We use this function to fetch the server data and return an array of multiple server value.

server($index[$xss_clean = NULL])

EXAMPLE

Here is simple demo of server class.

Controller code:-

Your controller code now look like this:-

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class input_controller extends CI_Controller 
{
public function input()
{
$server = $this-?>input->server(array('SERVER_PROTOCOL', 'REQUEST_URI'));
print_r($server);
}}
?>

Output will be like this.

8. Set cookies.

This method is used to set cookies containing specific value. We can pass the information in array and discreate parameters.

set_cookie($name = ''[$value = ''[$expire = ''[$domain = ''[$path = '/'[$prefix = ''[$secure = NULL[$httponly = NULL]]]]]]])

EXAMPLE

Here is simple example of set cookies class.

Controller code:-

Array method:-:-

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class input_controller extends CI_Controller 
{
public function arraycookies()
{
$cookie = array(
'name'   =?> 'cookies',
'value'  => 'Tutorialsplane.com',
'expire' => '3600');
$this->input->set_cookie($cookie);
echo get_cookie('cookies');
}}
?>

Output will be like this.

9. Valid IP address.

We use this function to check IP address whether it is valid or not.

valid_ip($ip[$which = ''])

EXAMPLE

Here is simple demo of valid ip class.

Controller code:-

Your controller code now look like this:-

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class input_controller extends CI_Controller 
{
public function validip()
	{
		$ip = "45.248.27.20";
		if ( ! $this-?>input->valid_ip($ip, 'ipv4'))
		{
				echo 'Not Valid';
		}
		else
		{
				echo 'Valid';
		}
		
	}
}
?>

Output will be like this.

10. User agent.

Its return all information about user web browser.

user_agent([$xss_clean = NULL])

EXAMPLE

Here is simple example of user agent class.

Controller code:-

Your controller code now look like this:-

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class input_controller extends CI_Controller 
{
public function useragent()
	{
		echo $this-?>input->user_agent();
		
	}
}
?>

Output will be like this.

11. Request header.

We use this function to return an array of HTTP request header.

request_headers([$xss_clean = FALSE])

EXAMPLE

Here is simple demo of request header class.

Controller code:-

Your controller code now look like this:-

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class input_controller extends CI_Controller 
{
public function requestHeader()
{
$headers = $this-?>input->request_headers();
print_r($headers);
}
}
?>

Output will be like this.

12. Method.

We use this function to set value in upper or lowercase.

method([$upper = FALSE])

EXAMPLE

Here is simple demo of method class.

Controller code:-

Your controller code now look like this:-

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class input_controller extends CI_Controller 
{
public function method()
{
echo $this-?>input->method(TRUE);
echo "<br/>";
echo $this->input->method(FALSE);
echo "<br/>";
echo $this->input->method();
}
}
?>

Output will be like this.

Models

Connect Database

Helpers

Libraries

Helper Reference

Library Reference

Database Reference