Common Functions in Codeigniter

Common Functions

CodeIgniter uses a few functions for its operation that are globally defined, and are available to you at any point. These do not require loading any libraries or helpers.

is_php($version)

Parameters: $version (string) – Version number

Returns: TRUE if the running PHP version is at least the one specified or FALSE if not

Return type: bool

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

Example:

Returns boolean TRUE if the installed version of PHP is equal to or greater than the supplied version number. Returns FALSE if the installed version of PHP is lower than the supplied version number.

is_really_writable($file)

Parameters: $file (string) – File path

Returns: TRUE if the path is writable, FALSE if not

Return type: bool

is_writable() returns TRUE on Windows servers when you really can’t write to the file as the OS reports to PHP as FALSE only if the read-only attribute is marked.

This function determines if a file is actually writable by attempting to write to it first. Generally only recommended on platforms where this information may be unreliable.

Example:

config_item($key)

Parameters: $key (string) – Config item key

Returns: Configuration key value or NULL if not found

Return type: mixed

The Config Library is the preferred way of accessing configuration information, however config_item() can be used to retrieve single keys. See Config Library documentation for more information.

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

Parameters: 1) $code (int) – HTTP Response status code
2) $text (string) – A custom message to set with the status code

Return type: void

Permits you to manually set a server status header. Example:

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

Parameters: 1) $str (string) – Input string
2) $url_encoded (bool) – Whether to remove URL-encoded characters as well

Returns: Sanitized string

Return type: string

This function prevents inserting NULL characters between ASCII characters, like Java\0script.

Example:

[php]
remove_invisible_characters(‘Java\\0script’);
// Returns: ‘Javascript’
/php]

html_escape($var)

Parameters: $var (mixed) – Variable to escape (string or array)

Returns: HTML escaped string(s)

Return type: mixed

This function acts as an alias for PHP’s native htmlspecialchars() function, with the advantage of being able to accept an array of strings.

It is useful in preventing Cross Site Scripting (XSS).

get_mimes()

Returns: An associative array of file types

Return type: array

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

is_https()

Returns: TRUE if currently using HTTP-over-SSL, FALSE if not

Return type: bool

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

is_cli()

Returns: TRUE if currently running under CLI, FALSE otherwise

Return type: bool

Returns TRUE if the application is run through the command line and FALSE if not.

Note: This function checks both if the PHP_SAPI value is ‘cli’ or if the STDIN constant is defined.

function_usable($function_name)

Parameters: $function_name (string) – Function name

Returns: TRUE if the function can be used, FALSE if not

Return type: bool

Returns TRUE if a function exists and is usable, FALSE otherwise.

This function runs a function_exists() check and if the Suhosin extension is loaded, checks if it doesn’t disable the function being checked.

It is useful if you want to check for the availability of functions such as eval() and exec(), which are dangerous and might be disabled on servers with highly restrictive security policies.

Note: This function was introduced because Suhosin terminated script execution, but this turned out to be a bug. A fix has been available for some time (version 0.9.34), but is unfortunately not released yet.

Insert data using ajax into mysql database

This example will guide you how to insert data into mysql database using AJAX and PHP.

Follow the below steps :
1). Create a file named ‘form.php’ and copy and paste the below code into it.

2). Create a new file named ‘insertdata.php’, copy and paste the below code into it. This is the code used for inserting data into mysql database.

3). Copy and paste the below code in the ‘form.php’ under head tag :

Note : This script requires the ‘jquery.min.js’ to be included before the ajax request code.

Now, the complete code would be

That’s it, customize the code as per your requirement and enjoy.

How to input only number in Textbox using javascript

In this tutorial you will learn how to limit a Text box only for taking digit input using javascript. You can simply do it through doing the input type number but this is not helpful in certain conditions.

See the tutorial below.

Html and Javascript code to limit text box to receive Number inputs only

We are done just copy this code in an Html file and run. It includes the text box which will only accept Numbers only.

How to fix msvcr110.dll is missing from your computer issue

Users face this problem after installing some specific softwares here are the solution for this.

About MSVCR110.dll

MSVCR110.dll is the Microsoft Visual C++ Redistributable dll that is needed for projects built with Visual Studio 2011. The dll letters spell this out.
MS = Microsoft, V = Visual, C = C++, R = Redistributable

This error appears when you wish to run a software which require the Microsoft Visual C++ Redistributable 2012.

Download the file MSVCR110.dll from here and copy it inside C:\Windows

Download Now

Send multiple data in ajax response

AJAX is an acronym for Asynchronous JavaScript and XML. It is a group of inter-related technologies like JavaScript, DOM, XML, HTML, CSS etc. AJAX allows you to send and receive data asynchronously without reloading the web page. You can use Ajax for getting data and showing it to your website without refreshing the web page.

In most condition people use to send single data in ajax response to check whether it satisfy the condition. In this post you will learn how to send multiple data in ajax response. We will use json to send and receive data where json_encode will be used to send the data from Back-End and parseJSON will be used to parse the data in Front-End.

We are taking php code as example here for Backend.

Back-End code for Json Encryption

Front-End code for Json Decryption

That’s it. Use this code to get Json response for getting multiple data to your ajax response. This code can be used on any platform.

Caching in Codeigniter

Web Page Caching

CodeIgniter lets you cache your pages in order to achieve maximum performance.

Although CodeIgniter is quite fast, the amount of dynamic information you display in your pages will correlate directly to the server resources, memory, and processing cycles utilized, which affect your page load speeds. By caching your pages, since they are saved in their fully rendered state, you can achieve performance that nears that of static web pages.

How Does Caching Work?

Caching can be enabled on a per-page basis, and you can set the length of time that a page should remain cached before being refreshed. When a page is loaded for the first time, the cache file will be written to your application/cache folder. On subsequent page loads the cache file will be retrieved and sent to the requesting user’s browser. If it has expired, it will be deleted and refreshed before being sent to the browser.

Enabling Caching

To enable caching, put the following tag in any of your controller methods:

Where $n is the number of minutes you wish the page to remain cached between refreshes.

The above tag can go anywhere within a method. It is not affected by the order that it appears, so place it wherever it seems most logical to you. Once the tag is in place, your pages will begin being cached.

Important: Because of the way CodeIgniter stores content for output, caching will only work if you are generating display for your controller with a view.

Important: If you change configuration options that might affect your output, you have to manually delete your cache files.

Note: Before the cache files can be written you must set the file permissions on your application/cache/ directory such that it is writable.

Deleting Caches

If you no longer wish to cache a file you can remove the caching tag and it will no longer be refreshed when it expires.

Note: Removing the tag will not delete the cache immediately. It will have to expire normally.

If you need to manually delete the cache, you can use the delete_cache() method: