php - 引用 : What is a perfect code sample using the MySQL extension?

标签 php mysql security sql-injection

很难说出这里问的是什么。这个问题是模棱两可的、含糊的、不完整的、过于宽泛或修辞的,不能以其目前的形式得到合理的回答。如需帮助澄清此问题以便可以重新打开,visit the help center .




9年前关闭。




This is to create a community learning resource. The goal is to have examples of good code that do not repeat the awful mistakes that can so often be found in copy/pasted PHP code. I have requested it be made Community Wiki.

This is not meant as a coding contest. It's not about finding the fastest or most compact way to do a query - it's to provide a good, readable reference especially for newbies.


每天,都会有大量问题涌入使用 mysql_* 的非常糟糕的代码片段中。 Stack Overflow 上的函数族。虽然通常最好将这些人引导到 PDO,但有时这既不可能(例如继承的遗留软件)也不是现实的期望(用户已经在他们的项目中使用它)。
使用 mysql_* 的代码的常见问题库包括:
  • 值中的 SQL 注入(inject)
  • LIMIT 子句和动态表名中的 SQL 注入(inject)
  • 没有错误报告(“为什么这个查询不起作用?”)
  • 破错误报告(即即使代码投入生产也总是出现错误)
  • 值输出中的跨站点脚本 (XSS) 注入(inject)

  • 让我们编写一个 PHP 代码示例,使用 mySQL_* family of functions 执行以下操作:
  • 接受两个 POST 值,id (数字)和 name (一个字符串)
  • 对表执行 UPDATE 查询 tablename ,更改 name ID 为 id 的行中的列
  • 失败时,优雅地退出,但仅在生产模式下显示详细错误。 trigger_error()就足够了;或者使用您选择的方法
  • 输出消息“$name 更新”。

  • 还有不是 显示上面列出的任何弱点。
    应该是 尽可能简单 .理想情况下,它不包含任何函数或类。目标不是创建复制/可粘贴库,而是创建 显示使数据库查询安全所需的最少操作。
    好评加分。
    目标是使这个问题成为用户在遇到代码错误(即使它根本不是问题的重点)或遇到失败的查询时可以链接到的资源知道如何修复它。
    抢先 PDO 讨论:
    是的,通常最好将写这些问题的人引导到 PDO。当它是一种选择时,我们应该这样做。然而,这并不总是可能的 - 有时,提问者正在处理遗留代码,或者已经在这个库上取得了长足的进步,现在不太可能改变它。另外,mysql_*如果使用得当,函数族是完全安全的。所以请不要在这里回答“使用 PDO”。

    最佳答案

    我的刺。尽量保持简单,同时仍然保持一些现实世界的便利。

    处理 unicode 并使用松散比较以提高可读性。对人好点 ;-)

    <?php
    
    header('Content-type: text/html; charset=utf-8');
    error_reporting(E_ALL | E_STRICT);
    ini_set('display_errors', 1);
    // display_errors can be changed to 0 in production mode to
    // suppress PHP's error messages
    
    /*
    Can be used for testing
    $_POST['id'] = 1;
    $_POST['name'] = 'Markus';
    */
    
    $config = array(
        'host' => '127.0.0.1', 
        'user' => 'my_user', 
        'pass' => 'my_pass', 
        'db' => 'my_database'
    );
    
    # Connect and disable mysql error output
    $connection = @mysql_connect($config['host'], 
        $config['user'], $config['pass']);
    
    if (!$connection) {
        trigger_error('Unable to connect to database: ' 
            . mysql_error(), E_USER_ERROR);
    }
    
    if (!mysql_select_db($config['db'])) {
        trigger_error('Unable to select db: ' . mysql_error(), 
            E_USER_ERROR);
    }
    
    if (!mysql_set_charset('utf8')) {
        trigger_error('Unable to set charset for db connection: ' 
            . mysql_error(), E_USER_ERROR);
    }
    
    $result = mysql_query(
        'UPDATE tablename SET name = "' 
        . mysql_real_escape_string($_POST['name']) 
        . '" WHERE id = "' 
        . mysql_real_escape_string($_POST['id']) . '"'
    );
    
    if ($result) {
        echo htmlentities($_POST['name'], ENT_COMPAT, 'utf-8') 
            . ' updated.';
    } else {
        trigger_error('Unable to update db: ' 
            . mysql_error(), E_USER_ERROR);
    }
    

    关于php - 引用 : What is a perfect code sample using the MySQL extension?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6198104/

    相关文章:

    mysql - SQL 错误代码 1066 唯一别名

    MySQL AVG 和分组

    security - 在所有节点上使用相同主体的Hadoop安全性

    python - pypi 包装和 secret : why does a github--intended . travis.yml 持有密码?

    php - 如何高度模式 4 :3 works in wordpress?

    php - PHP 的 hash_file 是在内部流式传输吗?

    php - Mysqli fetch_assoc() 循环与 Mysqli fetch_all() 数据库加载?

    c# - 在不同的独立 PC 上运行 C# 项目?

    php - 从表的单列中获取数据并将这些数据保存在每个变量中

    php - 代码点火器安全