如何防止 Wordpress 中的 SQL 注入? [英] How to prevent SQL Injection in Wordpress?

查看:60
本文介绍了如何防止 Wordpress 中的 SQL 注入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用以下查询通过 php 获取 mysql 中的值:

I'm currently using the following query to get values in mysql using php:

代码运行正常,但现在我担心 sql 注入.

The code is working, but now I'm worried about sql injections.

如何防止SQL注入?

<?php include_once("wp-config.php");
@$gameid = $_GET['gameid'];

global $wpdb;
$fivesdrafts = $wpdb->get_results( 
    "
    SELECT ID
    FROM $wpdb->posts
    WHERE  ID = ".$gameid." 

    "
);
?>

这样安全吗?

<?php include_once("wp-config.php");
@$gameid = mysql_real_escape_string($_GET['gameid']);

global $wpdb;
$fivesdrafts = $wpdb->get_results(
$wpdb->prepare(
    "
    SELECT ID
    FROM $wpdb->posts
    WHERE  ID = %d", ".$gameid.")
);
?>

推荐答案

来自 WordPress Codex 关于保护针对 SQL 注入攻击的查询:

<?php $sql = $wpdb->prepare( 'query' , value_parameter[, value_parameter ... ] ); ?>

如果您再向下滚动一点,就会看到示例.

If you scroll down a bit farther, there are examples.

您还应该阅读 数据库验证文档 以更全面地了解 WordPress 中的 SQL 转义.

You should also read the database validation docs for a more thorough overview of SQL escaping in WordPress.

这篇关于如何防止 Wordpress 中的 SQL 注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆