Compatibility Functions in Codeigniter

Compatibility Functions

CodeIgniter provides a set of compatibility functions that enable you to use functions what are otherwise natively available in PHP, but only in higher versions or depending on a certain extension.

Being custom implementations, these functions will also have some set of dependencies on their own, but are still useful if your PHP setup doesn’t offer them natively.

Note: Much like the common functions, the compatibility functions are always available, as long as their dependencies are met.

Password Hashing

This set of compatibility functions offers a “backport” of PHP’s standard Password Hashing extension that is otherwise available only since PHP 5.5.

Dependencies

1) PHP 5.3.7
2) CRYPT_BLOWFISH support for crypt()

Constants

1) PASSWORD_BCRYPT
2) PASSWORD_DEFAULT

Function reference

Parameters: 1) $password (string) – Plain-text password
2) $algo (int) – Hashing algorithm
3) $options (array) – Hashing options

Returns: Hashed password or FALSE on failure

Return type: string

For more information, please refer to the PHP manual for password_hash().

Unless you provide your own (and valid) salt, this function has a further dependency on an available CSPRNG source. Each of the following would satisfy that: – mcrypt_create_iv() with MCRYPT_DEV_URANDOM – openssl_random_pseudo_bytes() – /dev/arandom – /dev/urandom

Parameters: 1) $hash (string) – Password hash
2) $algo (int) – Hashing algorithm
3) $options (array) – Hashing options

Returns: TRUE if the hash should be rehashed to match the given algorithm and options, FALSE otherwise

Return type: bool

For more information, please refer to the PHP manual for password_needs_rehash().

Parameters: 1) $password (string) – Plain-text password
2) $hash (string) – Password hash

Returns: TRUE if the password matches the hash, FALSE if not

Return type: bool

For more information, please refer to the PHP manual for password_needs_rehash().

Hash (Message Digest)

This compatibility layer contains backports for the hash_equals() and hash_pbkdf2() functions, which otherwise require PHP 5.6 and/or PHP 5.5 respectively.

Dependencies

1) None

Function reference

Parameters: 1) $known_string (string) – Known string
2) $user_string (string) – User-supplied string

Returns: TRUE if the strings match, FALSE otherwise

Return type: string

For more information, please refer to the PHP manual for hash_equals().

Multibyte String

This set of compatibility functions offers limited support for PHP’s Multibyte String extension. Because of the limited alternative solutions, only a few functions are available.

When a character set parameter is ommited, $config[‘charset’] will be used.

Dependencies

1) iconv extension

Important: This dependency is optional and these functions will always be declared. If iconv is not available, they WILL fall-back to their non-mbstring versions.

Important: Where a character set is supplied, it must be supported by iconv and in a format that it recognizes.

Note: For you own dependency check on the actual mbstring extension, use the MB_ENABLED constant.

Function reference

Parameters: 1) $str (string) – Input string
2) $encoding (string) – Character set
Returns: Number of characters in the input string or FALSE on failure

Return type: string

For more information, please refer to the PHP manual for mb_strlen().

Parameters: 1) $haystack (string) – String to search in
2) $needle (string) – Part of string to search for
3)$offset (int) – Search offset
4) $encoding (string) – Character set

Returns: Numeric character position of where $needle was found or FALSE if not found

Return type: mixed

For more information, please refer to the PHP manual for mb_strpos().

Parameters: 1) $str (string) – Input string
2) $start (int) – Position of first character
3) $length (int) – Maximum number of characters
4) $encoding (string) – Character set

Returns: Portion of $str specified by $start and $length or FALSE on failure

Return type: string

For more information, please refer to the PHP manual for mb_substr().

Standard Functions

This set of compatibility functions offers support for a few standard functions in PHP that otherwise require a newer PHP version.

Dependencies

1) None

Function reference

Parameters: 1) $array (array) – Array to fetch results from
2) $column_key (mixed) – Key of the column to return values from
3) $index_key (mixed) – Key to use for the returned values

Returns: An array of values representing a single column from the input array

Return type: array

For more information, please refer to the PHP manual for array_column().

Parameters: $data (array) – Hexadecimal representation of data

Returns: Binary representation of the given data

Return type: string

For more information, please refer to the PHP manual for hex2bin().