duanlu7680 2011-07-01 18:20
浏览 528
已采纳

PHP和Ajax字符串输入/输出处理场景

I'm still new to String Processing in PHP. Below is a diagram of what I am currently doing. Ultimately, I would like to get a generic methodology for handling strings in the below scenarios. Note that the text tends to be a lot of math symbols and code syntax in this scenario.

The strings and integer are input via a standard HTML-based form (I forgot to mention that in the diagram).

Step A currently uses: mysql_real_escape_string(input);

Step B currently uses:

  • htmlentities($string2)
  • nothing for $string1
  • nothing for the integer

Questions:

  1. Regarding MySQL injection, is mysql_real_escape_string sufficient to guard against this?
  2. I still need to finish processing the output for String1. Note how the text is actually used in two different places: HTML and Canvas. htmlentities at step B would make code syntax appear properly on the HTML5 Canvas but not in the HTML. Vice-versa for leaving it out (HTML syntax breaks the Page). Is there a JavaScript function that is identical to PHP's htmlentities?
  3. int form should be validated to make sure it is an int.
  4. String2 ouputs "null" to the HTML when I use this character ('–') which is not the standard minus-sign character ('-').
  5. Magic Quotes is turned off. However, if I run the script on a server with it enabled, I need a short script that goes: IF(magic quotes is enabled){turn off magic quotes}.
  6. What did I forget in regard to form validation?

If my approach is totally wrong, set me straight and help me get this straightened out once and for all. You can describe your solution in terms of A, B, C, D and E if you think that is helpful. Thanks in advance.

String Processing Diagram

  • 写回答

3条回答 默认 最新

  • douchen9855 2011-07-01 18:39
    关注
    1. mysql_real_escape_string() will properly escape your code
    2. (A) On the JavaScript end, jQuery's .text() method will properly escape code before outputting it to the page. (B) Check out PHP.js. It's a JavaScript library that replicates PHP functionality, and includes htmlentities() and htmlspecialchars(). http://phpjs.org/
    3. As for the integer, you can use the ctype_digits() function to ensure that it is a numeric value; and you can also use some form of type hinting: $int = (int) $int;.
    4. I agree with the gentleman who recommended trying htmlspecialchars().
    5. Try to keep magic quotes off. It's a horrible feature, and will be removed in a future release of PHP. If you need to include protection against magic quotes, try something similar to the code below. (see bottom of post)
    6. It's hard to say without seeing your code, but it seems like you are on the right track.

    -

    if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
        $func = function(&$val, $key) {
            if (!is_numeric($val)) {
                $val = stripslashes($val);
            }
        };
    
        array_walk_recursive($_GET, $func);
        array_walk_recursive($_POST, $func);
        array_walk_recursive($_COOKIE, $func);
    
        unset($func);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站