【php】assert函數的用法
http://www.douban.com/note/217557007/
2012-06-01 10:32:37
assert這個函數在php語言中是用來判斷一個表達式是否成立。返回true or false;
例如
<?php
$s = 123;
assert("is_int($s)");
?>
從這個例子可以看到字符串參數會被執行,這跟eval()類似。不過eval($code_str)只是執行符合php編碼規范的$code_str。
assert的用法卻更詳細一點。
assert_option()可以用來對assert()進行一些約束和控制;
默認值
ASSERT_ACTIVE=1 //Assert函數的開關
ASSERT_WARNING =1 //當表達式為false時,是否要輸出警告性的錯誤提示,issue a PHP warning for each failed assertion
ASSERT_BAIL= 0 //是否要中止運行;terminate execution on failed assertions
ASSERT_QUIET_EVAL= 0 //是否關閉錯誤提示,在執行表達式時;disable error_reporting during assertion expression evaluation
ASSERT_CALLBACK= (NULL) // 是否啟動回調函數 user function to call on failed assertions
如 果按照默認值來,在程序的運行過程中調用assert()來進行判斷表達式,遇到false時程序也是會繼續執行的,這在生產環境中這樣使用是不好的,而 在開發調試環境中,卻是一種debug的不錯的方式。特別是用上callback的方法,可以知道具體的出錯信息。例如
<?php
// Active assert and make it quiet
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);
// Create a handler function
function my_assert_handler($file, $line, $code)
{
echo "<hr>Assertion Failed:File '$file'<br />Line '$line'<br />Code '$code'<br /><hr />";
}
// Set up the callback
assert_options(ASSERT_CALLBACK, 'my_assert_handler');
// Make an assertion that should fail
assert('mysql_query("")');
?>
所以,php的官方文檔里頭是建議將assert用來進行debug,我們可以發現還有一個開關ASSERT_ACTIVE可以用來控制是否開啟debug。
現在問題就產生了,如果程序員在開發的時候在代碼中留下了很多assert(),然后在程序發布的時候關閉執行,設置assert_options(ASSERT_ACTIVE,0);這樣做是否可行?有沒有安全問題?
我 的建議是,既然assert主要作用是debug,就不要在程序發布的時候還留着它。在程序中用assert來對表達進行判斷是不明智的,原因上文說了, 一個是在生產環境中assert可能被disabled,所以assert不能被完全信任;二是assert()可以被繼續執行;而如果在生產環境讓 ASSERT_ACTIVE=1,那這個表達式字符串可以被執行本身就存在安全隱患。
例如
<?php
function fo(){
$fp = fopen("c:/test.php",'w');
fwrite($fp,"123");
fclose($fp);
return true;
}
assert("fo()");
?>
例如
<?php
$s = 123;
assert("is_int($s)");
?>
從這個例子可以看到字符串參數會被執行,這跟eval()類似。不過eval($code_str)只是執行符合php編碼規范的$code_str。
assert的用法卻更詳細一點。
assert_option()可以用來對assert()進行一些約束和控制;
默認值
ASSERT_ACTIVE=1 //Assert函數的開關
ASSERT_WARNING =1 //當表達式為false時,是否要輸出警告性的錯誤提示,issue a PHP warning for each failed assertion
ASSERT_BAIL= 0 //是否要中止運行;terminate execution on failed assertions
ASSERT_QUIET_EVAL= 0 //是否關閉錯誤提示,在執行表達式時;disable error_reporting during assertion expression evaluation
ASSERT_CALLBACK= (NULL) // 是否啟動回調函數 user function to call on failed assertions
如 果按照默認值來,在程序的運行過程中調用assert()來進行判斷表達式,遇到false時程序也是會繼續執行的,這在生產環境中這樣使用是不好的,而 在開發調試環境中,卻是一種debug的不錯的方式。特別是用上callback的方法,可以知道具體的出錯信息。例如
<?php
// Active assert and make it quiet
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);
// Create a handler function
function my_assert_handler($file, $line, $code)
{
echo "<hr>Assertion Failed:File '$file'<br />Line '$line'<br />Code '$code'<br /><hr />";
}
// Set up the callback
assert_options(ASSERT_CALLBACK, 'my_assert_handler');
// Make an assertion that should fail
assert('mysql_query("")');
?>
所以,php的官方文檔里頭是建議將assert用來進行debug,我們可以發現還有一個開關ASSERT_ACTIVE可以用來控制是否開啟debug。
現在問題就產生了,如果程序員在開發的時候在代碼中留下了很多assert(),然后在程序發布的時候關閉執行,設置assert_options(ASSERT_ACTIVE,0);這樣做是否可行?有沒有安全問題?
我 的建議是,既然assert主要作用是debug,就不要在程序發布的時候還留着它。在程序中用assert來對表達進行判斷是不明智的,原因上文說了, 一個是在生產環境中assert可能被disabled,所以assert不能被完全信任;二是assert()可以被繼續執行;而如果在生產環境讓 ASSERT_ACTIVE=1,那這個表達式字符串可以被執行本身就存在安全隱患。
例如
<?php
function fo(){
$fp = fopen("c:/test.php",'w');
fwrite($fp,"123");
fclose($fp);
return true;
}
assert("fo()");
?>