CodeIgniter 公共函數(shù)

2018-07-21 15:37 更新

公共函數(shù)

CodeIgniter 定義了一些全局的函數(shù),你可以在任何地方使用它們,并且不需要加載任何 類庫(kù)或輔助函數(shù)。

is_php($version)

參數(shù):

  • $version (string) -- Version number

返回:

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

返回類型:

bool

判斷當(dāng)前運(yùn)行的 PHP 版本是否高于或等于你提供的版本號(hào)。

例如:

if (is_php('5.3'))
{
    $str = quoted_printable_encode($str);
}

如果當(dāng)前運(yùn)行的 PHP 版本等于或高于提供的版本號(hào),該函數(shù)返回布爾值 TRUE ,反之則返回 FALSE 。

is_really_writable($file)

參數(shù):

  • $file (string) -- File path

返回: TRUE if the path is writable, FALSE if not

返回類型: bool

在 Windows 服務(wù)器上只有當(dāng)文件標(biāo)志了只讀屬性時(shí),PHP 的 is_writable() 函數(shù)才返回 FALSE , 其他情況都是返回 TRUE ,即使文件不是真的可寫也返回 TRUE 。

這個(gè)函數(shù)首先嘗試寫入該文件,以此來(lái)判斷該文件是不是真的可寫。通常只在 is_writable() 函數(shù) 返回的結(jié)果不準(zhǔn)確的平臺(tái)下才推薦使用該函數(shù)。

例如:

if (is_really_writable('file.txt'))
{
    echo "I could write to this if I wanted to";
}
else
{
    echo "File is not writable";
}

注解

更多信息,參看 PHP bug #54709 。

config_item($key)

參數(shù):

  • $key (string) -- Config item key

返回:

Configuration key value or NULL if not found

返回類型:

mixed

訪問(wèn)配置信息最好的方式是使用 配置類 ,但是,你也可以通過(guò) config_item() 函數(shù)來(lái)訪問(wèn)單個(gè)配置項(xiàng),更多信息,參看 配置類

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

參數(shù):

  • $code (int) -- HTTP Reponse status code
  • $text (string) -- A custom message to set with the status code

返回類型:

void

用于手動(dòng)設(shè)置服務(wù)器的 HTTP 狀態(tài)碼,譬如:

set_status_header(401);
// Sets the header as:  Unauthorized

查看這里 有一份狀態(tài)碼的完整清單。

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

參數(shù):

  • $str (string) -- Input string
  • $url_encoded (bool) -- Whether to remove URL-encoded characters as well

返回:

Sanitized string

返回類型:

string

這個(gè)函數(shù)防止在 ASCII 字符串中插入空字符,例如:Java\0script 。

舉例:

remove_invisible_characters('Java\\0script');
// Returns: 'Javascript'

html_escape($var)

參數(shù):

  • $var (mixed) -- Variable to escape (string or array)

返回:

HTML escaped string(s)

返回類型:

mixed

這個(gè)函數(shù)類似于 PHP 原生的 htmlspecialchars() 函數(shù),只是它除了可以接受字符串參數(shù)外,還可以接受數(shù)組參數(shù)。

它在防止 XSS 攻擊時(shí)很有用。

get_mimes()

返回: An associative array of file types
返回類型: array

這個(gè)函數(shù)返回 application/config/mimes.php 文件中定義的 MIME 數(shù)組的 引用 。

is_https()

返回: TRUE if currently using HTTP-over-SSL, FALSE if not
返回類型: bool

該函數(shù)在使用 HTTPS 安全連接時(shí)返回 TRUE ,沒有使用 HTTPS(包括非 HTTP 的請(qǐng)求)則返回 FALSE 。

is_cli()

返回: TRUE if currently running under CLI, FALSE otherwise
返回類型: bool

當(dāng)程序在命令行下運(yùn)行時(shí)返回 TRUE ,反之返回 FALSE 。

注解

該函數(shù)會(huì)檢查 PHP_SAPI 的值是否是 'cli' ,或者是否定義了 STDIN 常量。

function_usable($function_name)

參數(shù):

  • $function_name (string) -- Function name

返回:

TRUE if the function can be used, FALSE if not

返回類型:

bool

檢查一個(gè)函數(shù)是否可用,可用返回 TRUE ,否則返回 FALSE 。

該函數(shù)直接調(diào)用 function_exists() 函數(shù),并檢查當(dāng)前是否加載了 Suhosin 擴(kuò)展  ,如果加載了 Suhosin ,檢查函數(shù)有沒有被它禁用。

這個(gè)函數(shù)在你需要檢查某些函數(shù)的可用性時(shí)非常有用,例如 eval() 和 exec() 函數(shù)是非常危險(xiǎn)的,可能會(huì)由于服務(wù)器的安全策略被禁用。

注解

之所以引入這個(gè)函數(shù),是由于 Suhosin 的某個(gè) bug 可能會(huì)終止腳本的執(zhí)行, 雖然這個(gè) bug 已經(jīng)被修復(fù)了(版本 0.9.34),但可惜的是還沒發(fā)布。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)