zoukankan      html  css  js  c++  java
  • PHP MySQL 安全方案

     1 转义与清除转义

    // 对 用户提交的数据 ' "  进行转义
    if ( get_magic_quotes_gpc() )
    {
        function del_magic_quotes($value)
        {
            $value = is_array($value) ? array_map('del_magic_quotes', $value) : stripslashes($value);
     
            return $value;
        }  
     
        $_POST    = del_magic_quotes( $_POST );
        $_GET     = del_magic_quotes( $_GET );
        $_COOKIE  = del_magic_quotes( $_COOKIE );
        $_REQUEST = del_magic_quotes( $_REQUEST );
    }
     
    function add_magic_quotes( $array ) {
        foreach ( (array) $array as $k => $v ) {
            if ( is_array( $v ) ) {
                $array[$k] = add_magic_quotes( $v );
            } else {
                $array[$k] = addslashes( $v );
            }
        }
        return $array;
    }
     
    $_GET     = add_magic_quotes( $_GET );
    $_POST    = add_magic_quotes( $_POST );
    $_COOKIE  = add_magic_quotes( $_COOKIE );
    $_REQUEST = add_magic_quotes( $_REQUEST );

    2 对于用户名这样的字段,输入时候,检查不允许有空格,而且必须是字母数字下划线或划线这四种,用正则检查

    所有ID为数字的变量,必须检查是否为数字,并将变量强制转换成数字

    4  有长度限制的一定要加入长度限制

    5 apache,php,mysql不要以系统用户运行

    系统的所有错误信息必须关闭或者屏蔽,用日志记录报错

    屏蔽非主流浏览器的user-agent

    8 普通单个变量检查安全代码

    function check_input($value)
    {
    // 去除斜杠
    if (get_magic_quotes_gpc())
      {
      $value = stripslashes($value);
      }
    // 如果不是数字则加引号
    if (!is_numeric($value))
      {
      $value = "'" . mysql_real_escape_string($value) . "'";
      }
    return $value;
    }
    
    $con = mysql_connect("localhost", "hello", "321");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    // 进行安全的 SQL
    $user = check_input($_POST['user']);
    $pwd = check_input($_POST['pwd']);
    $sql = "SELECT * FROM users WHERE
    user=$user AND password=$pwd";
    
    mysql_query($sql);
    
    mysql_close($con);

     

  • 相关阅读:
    Luogu P4053 [JSOI2007]建筑抢修
    CF894E Ralph and Mushrooms
    Boruvka
    Codeforces Beta Round #25 (Div. 2 Only) C. Roads in Berland
    HDU 3714/UVA1476 Error Curves
    HDU 5410 CRB and His Birthday
    HDU 1796 How many integers can you find
    UVA 11624 Fire!
    POJ 3279 Dungeon Master
    POJ 1321 棋盘问题
  • 原文地址:https://www.cnblogs.com/wangxusummer/p/4778468.html
Copyright © 2011-2022 走看看