duanqianpu6499 2012-03-13 17:52
浏览 53
已采纳

$ _GET ['param_name']是否可能在数组返回时遇到麻烦?

I'm new to PHP and this question seems stupid.

But I'm really confused with the associative array $_GET, which can help me access the all parameters that has been sent via URL.

Suppose I'm expecting a string of product_ID, and write in the code like this :

$id = $_GET['prod_id'];
Get_Data($id);

While Get_Data() is a function that expects the only parameter to be string, but not array. What if some bad guy type in the url something like this :

.../product.php?prod_id[]=1&prod_id[]=2

The method using $_GET['prod_id'] will return an array(1,2) instead of a string. This can lead to some really bad trouble in my application.

Now, the question : Is there a global way to avoid the case above ?


[EDIT]

Sometimes I want to get array from $_GET['prod_id'] instead of string (ex: getting data from a multi-selectbox, where users can pick more than 1 product)

Is it feasible for me to check if the returning array is in correct structure (1 dimensional array, with innocent data) or has been cheated by some bad guy like this :

.../product.php?prod_id[a]=1&prod_id[b]=2&prod_id[c]=3&prod_id[d]=4

I think it's very easy to pass an array with complex structure to php $_GET, but very hard for coder to check if it's the correct structure they needed.

Can you please enlighten me? Thanks !

  • 写回答

4条回答 默认 最新

  • dqy0707 2012-03-13 17:56
    关注

    The answer is: Validation

    One usually use the filter_input() function.

    http://ca3.php.net/filter

    If sometimes you want it to be an array, use a condition if (is_array($_GET['prod_id'])) and use different set of validations.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题