CodeIgniter - Cookie Management

CodeIgniter - Cookie Management

CodeIgniter - Cookie Management

A cookie is a small piece of data sent from a web server to be stored on a client's computer. CodeIgniter has one helper called Cookie Helper for managing cookies.

Syntax

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

parameters

  • $name ( mixed ) - the name of the cookie, or an associative array of all options available to this function
  • $value ( string ) - cookie value
  • $expire ( int ) - number of seconds before expiration
  • $domain ( string ) — Cookie domain (usually: .yourdomain.com)
  • $path ( string ) - path to cookie
  • $prefix ( string ) - cookie name prefix
  • $ secure ( bool ) - whether to send the cookie only over HTTPS
  • $httponly ( bool ) - whether to hide the cookie from JavaScript

return type

invalid

Syntax

parameters

$name ( mixed ) - the name of the cookie, or an associative array of all options available to this function

$value ( string ) - cookie value

$expire ( int ) - number of seconds before expiration

$domain ( string ) — Cookie domain (usually: .yourdomain.com)

$path ( string ) - path to cookie

$prefix ( string ) - cookie name prefix

$ secure ( bool ) - whether to send the cookie only over HTTPS

$httponly ( bool ) - whether to hide the cookie from JavaScript

return type

In the set_cookie() function, we can pass all values ​​in two ways. First, only an array can be passed, and second, individual parameters can also be passed.

Syntax

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

parameters

  • $index ( string ) - cookie name
  • $ xss_clean ( bool ) - whether to apply XSS filtering to the return value

Return

Cookie value or NULL if not found

return type

mixed

Syntax

parameters

$index ( string ) - cookie name

$ xss_clean ( bool ) - whether to apply XSS filtering to the return value

Return

return type

The get_cookie() function is used to get the cookie set using the set_cookie() function.

Syntax

delete_cookie( $name [, $domain=" [, $path='/' [, $prefix=" ]]]]])

parameters

  • $name ( string ) - cookie name
  • $domain ( string ) — Cookie domain (usually: .yourdomain.com)
  • $path ( string ) - path to cookie
  • $prefix ( string ) - cookie name prefix

return type

invalid

Syntax

parameters

$name ( string ) - cookie name

$domain ( string ) — Cookie domain (usually: .yourdomain.com)

$path ( string ) - path to cookie

$prefix ( string ) - cookie name prefix

return type

The delete_cookie() function is used to delete the cookie().

example

Create a controller named Cookie_controller.php and save it to application/controller/Cookie_controller.php

Create a view file named Cookie_view.php and save it to application/views/Cookie_view.php

 

  

      <metacharset="utf-8">

     

  

        

  

      Click Here to view the cookie.

      Click Here to delete the cookie.

  

        

Edit the route.php file in application/config/rout.php to add the route for the above controller and add the following line at the end of the file.

$route['cookie'] = "Cookie_controller";

$route['cookie/display'] = "Cookie_controller/display_cookie";

$route['cookie/delete'] = "Cookie_controller/deletecookie";

After that, you can execute the following URL in a browser to run the example.

http://yoursite.com/index.php/cookies

It will give output as shown in the following screenshot.

CodeIgniter - Common Functions

Library functions and CodeIgniter helper functions must be initialized before they can be used, but there are some common functions that do not need to be initialized.

These common functions and their descriptions are given below.

Syntax

is_php( $version )

parameters

$version ( string ) - version number

Return

TRUE if the running PHP version is at least specified, or FALSE if not

return type

invalid

Description

Determines if the version of PHP being used is greater than the specified version number.

$version ( string ) - version number

Syntax

is_really_writable( $file )

parameters

$file ( string ) — File path

Return

TRUE if the path is writable, FALSE if not

return type

BOOL

Description

checks if the file is writable or not.

$file ( string ) — File path

Syntax

config_item ( $ key )

parameters

$key ( string ) - configuration element key

Return

The value of the configuration key, or NULL if not found

return type

mixed

Description

This function is used to get a configuration element

$key ( string ) - configuration element key

Syntax

set_status_header( $code [, $text=" ])

parameters

$code ( int ) - HTTP response status code

$text ( string ) - custom message to install with status code

Return

return type

invalid

Description

This feature allows you to manually set the server status header.

$code ( int ) - HTTP response status code

$text ( string ) - custom message to install with status code

Syntax

remove_invisible_characters( $str [, $url_encoded = TRUE ])

parameters

$str ( string ) - input string

$ url_encoded ( bool ) - whether to strip URL encoded characters as well

Return

Sanitized string

return type

line

Description

This function prevents NULL characters from being inserted between ASCII characters

$str ( string ) - input string

$ url_encoded ( bool ) - whether to strip URL encoded characters as well

Syntax

html_escape( $var )

parameters

$ var ( mixed ) - variable to escape (string or array)

Return

HTML escaped string(s)

return type

mixed

Description

This function acts like PHP's built-in htmlspecialchars() function.

$ var ( mixed ) - variable to escape (string or array)

Syntax

get_mimes()

Return

Associative array of file types

return type

array

Description

This function returns a reference to the MIME array from application/config/mimes.php .

 

Syntax

is_https()

Return

TRUE if HTTP-over-SSL is currently in use, FALSE if not

return type

BOOL

Description

Returns TRUE if a secure (HTTPS) connection is used, and FALSE in any other case (including non-HTTP requests).

 

Syntax

is_cli()

Return

TRUE if currently running under CLI, FALSE otherwise

return type

BOOL

Description

Returns TRUE if the application is run from the command line, FALSE otherwise.

 

Syntax

function_usable( $function_name )

parameters

$function_name ( string ) - function name

return type

BOOL

Description

Returns TRUE if the function exists and can be used, FALSE otherwise.

$function_name ( string ) - function name

Below is an example that demonstrates all of the above features.

example

Here we have created only one controller in which we will use the above functions. Copy the code below and save it to application/controller/CommonFun_Controller.php .

Edit the route.php file in application/config/rout.php to add the route for the above controller and add the following line at the end of the file.

$route['commonfunctions'] = 'CommonFun_Controller';

Type the following URL into your browser's address bar to run the example.

http://yoursite.com/index.php/commonfunctions