兼容性函数

CodeIgniter 提供了一系列兼容性函数可以让你使用,它们只有在高版本的 PHP 中才有, 或者需要依赖其他的扩展才有。

由于是自己实现的,这些函数本身也可能有它自己的依赖性,但如果你的 PHP 中不提供这些函数时, 这些函数还是有用的。

注解

公共函数 一样,兼容性函数也一直可以访问,只要满足了他们的依赖条件。

密码哈希

这几个兼容性函数移植了 PHP 标准的 密码哈希扩展 的实现, 这些函数只有在 PHP 5.5 以后的版本中才有。

依赖性

  • PHP 5.3.7
  • crypt() 函数需支持 CRYPT_BLOWFISH

常量

  • PASSWORD_BCRYPT
  • PASSWORD_DEFAULT

函数参考

password_get_info($hash)
参数:
  • $hash (string) -- Password hash
返回:

Information about the hashed password

返回类型:

array

更多信息,请参考 PHP 手册中的 password_get_info() 函数

password_hash($password, $algo[, $options = array()])
参数:
  • $password (string) -- Plain-text password
  • $algo (int) -- Hashing algorithm
  • $options (array) -- Hashing options
返回:

Hashed password or FALSE on failure

返回类型:

string

更多信息,请参考 PHP 手册中的 password_hash() 函数

注解

除非提供了你自己的有效的 Salt,该函数会依赖于一个可用的 CSPRNG 源(密码学安全的伪随机数生成器), 下面列表中的每一个都可以满足这点: - mcrypt_create_iv() with MCRYPT_DEV_URANDOM - openssl_random_pseudo_bytes() - /dev/arandom - /dev/urandom

password_needs_rehash()
参数:
  • $hash (string) -- Password hash
  • $algo (int) -- Hashing algorithm
  • $options (array) -- Hashing options
返回:

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

返回类型:

bool

更多信息,请参考 PHP 手册中的 password_needs_rehash() 函数

password_verify($password, $hash)
参数:
  • $password (string) -- Plain-text password
  • $hash (string) -- Password hash
返回:

TRUE if the password matches the hash, FALSE if not

返回类型:

bool

更多信息,请参考 PHP 手册中的 password_verify() 函数

哈希(信息摘要)

兼容性函数移植了 hash_equals()hash_pbkdf2() 的实现, 这两函数分别在 PHP 5.6 和 PHP 5.5 以后的版本中才有。

函数参考

hash_equals($known_string, $user_string)
参数:
  • $known_string (string) -- Known string
  • $user_string (string) -- User-supplied string
返回:

TRUE if the strings match, FALSE otherwise

返回类型:

string

更多信息,请参考 PHP 手册中的 hash_equals() 函数

hash_pbkdf2($algo, $password, $salt, $iterations[, $length = 0[, $raw_output = FALSE]])
参数:
  • $algo (string) -- Hashing algorithm
  • $password (string) -- Password
  • $salt (string) -- Hash salt
  • $iterations (int) -- Number of iterations to perform during derivation
  • $length (int) -- Output string length
  • $raw_output (bool) -- Whether to return raw binary data
返回:

Password-derived key or FALSE on failure

返回类型:

string

更多信息,请参考 PHP 手册中的 hash_pbkdf2() 函数

多字节字符串

这一系列兼容性函数提供了对 PHP 的 多字节字符串扩展 的有限支持, 由于可选的解决方法有限,所以只有几个函数是可用的。

注解

如果没有指定字符集参数,默认使用 $config['charset'] 配置。

依赖性

重要

这个依赖是可选的,无论 iconv 扩展是否存在,这些函数都已经定义了, 如果 iconv 扩展不可用,它们会降级到非多字节字符串的函数版本。

重要

当设置了字符集时,该字符集必须被 iconv 支持,并且要设置成它可以识别的格式。

注解

如果你需要判断是否支持真正的多字节字符串扩展,可以使用 MB_ENABLED 常量。

函数参考

mb_strlen($str[, $encoding = NULL])
参数:
  • $str (string) -- Input string
  • $encoding (string) -- Character set
返回:

Number of characters in the input string or FALSE on failure

返回类型:

string

更多信息,请参考 PHP 手册中的 mb_strlen() 函数

mb_strpos($haystack, $needle[, $offset = 0[, $encoding = NULL]])
参数:
  • $haystack (string) -- String to search in
  • $needle (string) -- Part of string to search for
  • $offset (int) -- Search offset
  • $encoding (string) -- Character set
返回:

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

返回类型:

mixed

更多信息,请参考 PHP 手册中的 mb_strpos() 函数

mb_substr($str, $start[, $length = NULL[, $encoding = NULL]])
参数:
  • $str (string) -- Input string
  • $start (int) -- Position of first character
  • $length (int) -- Maximum number of characters
  • $encoding (string) -- Character set
返回:

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

返回类型:

string

更多信息,请参考 PHP 手册中的 mb_substr() 函数

标准函数

这一系列兼容性函数提供了一些高版本的 PHP 中才有的标准函数。

函数参考

array_column(array $array, $column_key[, $index_key = NULL])
参数:
  • $array (array) -- Array to fetch results from
  • $column_key (mixed) -- Key of the column to return values from
  • $index_key (mixed) -- Key to use for the returned values
返回:

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

返回类型:

array

更多信息,请参考 PHP 手册中的 array_column() 函数

hex2bin($data)
参数:
  • $data (array) -- Hexadecimal representation of data
返回:

Binary representation of the given data

返回类型:

string

更多信息,请参考 PHP 手册中的 hex2bin() 函数