sql注入式漏洞是许多php程序的主要安全危害,产生的原因是在向数据库执行插入等语句时,者允许最终用户操作变量(例如根据表单提交内容显示相应信息),通常是_get、_post或_session等全局变量。
让我们看以下的代码:
以下为引用的内容: <?php query = "select news_title, news_text "; query .= "from news"; query .= "where news_id=". _get['id']; mysql_query(query); ?> |
以下为引用的内容: <?php if (!is_numeric(_get['id'])) { // id's not numeric? // kill the script before the query can run die("the id must be numeric!"); } query = "select news_title, news_text "; query .= "from news"; query .= "where news_id=". _get['id']; mysql_query(query); ?> |
以下为引用的内容: <?php // fix a _post variable called firstname for mysql firstname = _post['firstname']; if (get_magic_quotes_gpc()) { // if magic quotes is enabled - turn the string back into an unsafe string firstname = stripslashes(firstname); } // now convert the unsafe string into a mysql safe string firstname= mysql_real_escape_string(firstname); // firstname should now be safe to insert into a query ?> |
以下为引用的内容: <?php firstname = _post['firstname']; if (get_magic_quotes_gpc()) { // if magic quotes is enabled - turn the string back into an unsafe string firstname = stripslashes(firstname); } // now convert the unsafe string into a mysql safe string firstname = mysql_real_escape_string(firstname); // safe query mysql_query("insert into names values('". firstname ."')"); // page output should look proper echo "hello ". htmlentities(stripslashes(firstname)); ?> |
以下为引用的内容: <?php function verifyinput(input, forceint = false) { if (is_numeric(input)) { return input; } elseif (!forceint) { if (get_magic_quotes_gpc()) { // if magic quotes is enabled, get rid of those // pesky slashes input = stripslashes(input); } // convert the input variable into a mysql safe string. input = mysql_real_escape_string(input); return input; } else { // if input not an integer and forceint = true, // kill script die("invalid input"); } } // _post['name'] should be a string // _post['id'] should be an integer, if not the script dies id = _post['id']; name = _post['name']; query = "update users set name=". verifyinput(name) ." "; query .= "where id=". verifyinput(id, true); // query should be safe to run mysql_query(query); ?> |
新闻热点
疑难解答