gpt4 book ai didi

php - PHP 和 Mysql 脚本中哪一个更安全?

转载 作者:行者123 更新时间:2023-11-29 15:08:28 25 4
gpt4 key购买 nike

脚本 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.?>

最佳答案

都不是。两者是不一致的,并且为未过滤的数据提供了访问数据库的操作性。

过滤不是可选的。

不应该有一种方法可以在不进行过滤的情况下运行查询。如果您使用 php 5.2.x,请使用 SPL Filter清理数据的函数。

数据抽象层使这变得更容易。我喜欢zend框架的Zend_Db和 Zend_Db_Table 类。这些文档有具体的示例,展示了实际中的最佳用法。

在你的两个脚本中,我会选择#1。这个函数化代码将比脚本 #2 中的代码更容易维护。

关于php - PHP 和 Mysql 脚本中哪一个更安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1450459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com