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]])
  • Parameters.
  • $index (mixed) : POST parameter name
  • $xss_clean (bool) : Whether to apply XSS filtering
  • Returns : $_POST if no parameters supplied, otherwise the POST value if found or NULL if not
  • Return type : mixed

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]])
  • Parameters.
  • $index (mixed) : GET parameter name
  • $xss_clean (bool) : Whether to apply XSS filtering
  • Returns : $_GET if no parameters supplied, otherwise the GET value if found or NULL if not
  • Return type : mixed

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])
  • Parameters.
  • $index (mixed) : POST/GET parameter name
  • $xss_clean (bool) : Whether to apply XSS filtering
  • Returns : POST/GET value if found, NULL if not
  • Return type : mixed

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])
  • Parameters.
  • $index (mixed) : GET/POST parameter name
  • $xss_clean (bool) : Whether to apply XSS filtering
  • Returns : GET/POST value if found, NULL if not
  • Return type : mixed

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]])
  • Parameters.
  • $index (mixed) : COOKIE name
  • $xss_clean (bool) : Whether to apply XSS filtering
  • Returns : $_COOKIE if no parameters supplied, otherwise the COOKIE value if found or NULL if not
  • Returns type : String

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.

Codeigniter Input Class Library

7. Server.

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

server($index[$xss_clean = NULL])
  • Parameters.
  • $index (mixed) : Value name
  • $xss_clean (bool) : Whether to apply XSS filtering
  • Returns : $_SERVER item value if found, NULL if not
  • Returns type : mixed

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.

Codeigniter Input Class Library

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]]]]]]])
  • Parameters.
  • $name (mixed) : Cookie name or an array of parameters
  • $value (string) : Cookie value
  • $expire (int) : Cookie expiration time in seconds
  • $domain (string) : Cookie domain
  • $path (string) : Cookie path
  • $prefix (string) : Cookie name prefix
  • $secure (bool) : Whether to only transfer the cookie through HTTPS
  • $httponly (bool) : Whether to only make the cookie accessible for HTTP requests (no JavaScript)
  • Returns type : void

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.

Codeigniter Input Class Library

9. Valid IP address.

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

valid_ip($ip[$which = ''])
  • Parameters.
  • $ip (string) : IP address
  • $which (string) : IP protocol (‘ipv4’ or ‘ipv6’)
  • Returns : TRUE if the address is valid, FALSE if not
  • Returns type : Bool

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.

Codeigniter Input Class Library

10. User agent.

Its return all information about user web browser.

user_agent([$xss_clean = NULL])
  • Parameters.
  • $xss_clean (bool) : Whether to apply XSS filtering
  • Returns : User agent string or NULL if not set
  • Returns type : mixed

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.

Codeigniter Input Class Library

11. Request header.

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

request_headers([$xss_clean = FALSE])
  • Parameters.
  • $index (mixed) : Value name
  • $xss_clean (bool) : Whether to apply XSS filtering
  • Returns : An array of HTTP request headers
  • Returns type : array

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.

Codeigniter Input Class Library

12. Method.

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

method([$upper = FALSE])
  • Parameters.
  • $upper (bool) : Whether to return the request method name in upper or lower case
  • Returns : HTTP request method
  • Returns type : String

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.

Codeigniter Input Class Library

Advertisements

Add Comment

📖 Read More