脚本1更安全吗?
"; } else { queryMysql("CREATE TABLE $name($query)"); echo "Table '$name' created
"; } } function tableExists($name) { $result = queryMysql("SHOW TABLES LIKE '$name'"); return mysql_num_rows($result); } function queryMysql($query) { $result = mysql_query($query) or die(mysql_error()); return $result; } function destroySession() { $_SESSION=array(); if (session_id() != "" || isset($_COOKIE[session_name()])) setcookie(session_name(), '', time()-2592000, '/'); session_destroy(); } function sanitizeString($var) { $var = strip_tags($var); $var = htmlentities($var); $var = stripslashes($var); return mysql_real_escape_string($var); } function showProfile($user) { if (file_exists("$user.jpg")) echo "img src='$user.jpg' border='1' align='left' />"; $result = queryMysql("SELECT * FROM rnprofiles WHERE user='$user'"); if (mysql_num_rows($result)) { $row = mysql_fetch_row($result); echo stripslashes($row[1]) . "
"; } } ?>
或脚本2更安全吗?
MySQL Error: " . mysql_error()); // Print a message to the user, include the footer, and kill the script. include ('./includes/footer.htm'); exit(); } // End of mysql_select_db IF. } else { // If it couldn't connect to MySQL. // Print a message to the user, include the footer, and kill the script. trigger_error("Could not connect to MySQL!\n
MySQL Error: " . mysql_error()); include ('./includes/footer.htm'); exit(); } // End of $dbc IF. // Create a function for escaping the data. function escape_data ($data) { // Address Magic Quotes. if (ini_get('magic_quotes_gpc')) { $data = stripslashes($data); } // Check for mysql_real_escape_string() support. if (function_exists('mysql_real_escape_string')) { global $dbc; // Need the connection. $data = mysql_real_escape_string (trim($data), $dbc); } else { $data = mysql_escape_string (trim($data)); } // Return the escaped value. return $data; } // End of function. ?>
答案 0 :(得分:1)
都不是。它们都不一致,并为未经过滤的数据提供了进入数据库的机会。
过滤不是可选的。
如果没有进行过滤,则无法运行查询。 如果您使用的是PHP 5.2.x,请使用SPL Filter函数来清理数据。
数据抽象层使这更容易。我喜欢zend框架的Zend_Db和Zend_Db_Table类。这些文档的具体示例显示了最佳可能的用法。
在你的两个脚本中,我选择了#1。这个功能化的代码比脚本#2中的东西更容易维护。
答案 1 :(得分:0)
更安全的方法是使用MYSQLi或PDO。此外,MYSQL_CONNECT等将在未来版本的php中禁用。