简体   繁体   English

处理职位要求

[英]Handling Post Requests

I wrote this, and wanted to get everyones opinion. 我写了这篇,希望引起大家的意见。 I use this when I'm expecting a variable from a FORM submission. 当我期望FORM提交中有一个变量时,我会使用它。 Ie: 即:

<form method="post" action="index.php">
Username: <input type="text" name="username">
</form>


$username = get_request('username');

function get_request($name) {
    if(isset($_REQUEST[$name])) {
        //return mysql_real_escape_string(htmlentities($_REQUEST[$name]));
        return mysql_real_escape_string($_REQUEST[$name]);
    } else {
        return "";
    }
}

Nice, but shouldn't you want to return an error instead of an empty string if the $_REQUEST fails? 很好,但是如果$ _REQUEST失败,您是否不应该返回错误而不是空字符串? Otherwise good! 否则很好!

While mysql_real_escape_string() does a good job, you may wish to be stricter on what you return. 尽管mysql_real_escape_string()做得很好,但是您可能希望对返回的内容更加严格。 Eg if you only need alphanumeric characters it would be safer (and possibly faster) to do: 例如,如果您只需要字母数字字符,则这样做会更安全(甚至更快):

return preg_replace('/[^a-z0-9]/', '', $_REQUEST[$name]);

Or even use filter_var if you are running PHP 5.2+. 如果您正在运行PHP 5.2+,甚至可以使用filter_var

Also as mentioned above, you if you can easily use $_POST instead of $_REQUEST if you are only handling POST data. 同样如上所述,如果仅处理POST数据,则可以轻松使用$ _POST而不是$ _REQUEST。

Other than than that, keep up the good work! 除此之外,请继续努力! :) :)

It looks OK, except it won't help you against certain SQL injections. 看起来还可以,只是它不能帮助您进行某些SQL注入。 You should add another argument to your function that sanitizes input according to types, for instance to make sure your return is an integer or a floating point number. 您应该在函数中添加另一个根据类型对输入进行过滤的参数,例如,确保返回的值是整数或浮点数。

If for instance you have a pagination mechanism with a query SELECT * FROM tbl LIMIT 10, $page . 例如,如果您有一个带有查询SELECT * FROM tbl LIMIT 10, $page的分页机制SELECT * FROM tbl LIMIT 10, $page If $page = like 1; DROP TABLE tbl -- 如果$page = like 1; DROP TABLE tbl -- like 1; DROP TABLE tbl -- and it gets send through your function get_request() , it won't help you any bit. like 1; DROP TABLE tbl --它通过您的函数get_request()发送,这对您没有任何帮助。

Another thing is you should just return null instead of an empty string ( "" ) if it doesn't exist. 另一件事是,如果不存在,则应只返回null而不是空字符串( "" )。

If you for instance have a variable $x = "" , then isset($x) will return true , whereas it would return false with $x = null . 例如,如果您有一个变量$x = "" ,则isset($x)将返回true ,而在$x = null时将返回false。

And as other people have pointed out, you should distinguish between GET and POST . 正如其他人指出的那样,您应该区分GETPOST

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM