douni9620 2012-01-31 19:59
浏览 7
已采纳

PHP正确处理未分配的POSTS和GETS的方法

I recently installed the WAMP server package on my computer at work. When I first started messing around with PHP(not too long ago), I installed PHP, MySQL, and Apache manually. Well, the application I have been working on is now giving me a WHOLE BUNCH of Notice messages:

Notice: Undefined index: lookatLon in C:\wamp\www\coordinates.php on line 3

Well, as it turns out, I have learned that I am handling my assigning of $variable = $_REQUEST['whatever']; all wrong. For example... I have a script that accepts a $longitude and $latitude from a $_GET['longitude']; and $_GET['latitude'] .... but under certain circumstances, the page is loaded without any 'longitude=' or 'latitude=' in the url.

Now I am getting these notices that it isn't liking my unset variables.

So, my question is, what is the proper way to handle a situation, where sometimes there is GET or POST information available and sometimes there isn't. I just read that it is not a good idea to shut off the notices(which would surely fix my problem), but rather the solution is to code properly.

So what is the proper way to account for GET or POST data that may or may not be there, depending on the situation?

  • 写回答

5条回答 默认 最新

  • dpnv33177 2012-01-31 20:05
    关注

    To get values you have to initialize variables, like this

    $longitude = '';
    $latitude = '';
    if (isset($_GET['longitude'])) $longitude = $_GET['longitude'];
    if (isset($_GET['latitude'])) $latitude = $_GET['latitude'];
    

    and later use assigned variables

    To control the program flow you may query the request variable directly:

    if (isset($_GET['id'])) {
      //get data
    }
    
    • DO NOT use $_REQUEST. Know where your data from and don't let it interfere.
    • you may wish also do some validation on request variables.
    • you may keep variables in the array, if you gonna batch process them, to create an SQL statement, for example.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法