PHP验证查询

时间:2013-07-20 02:10:21

标签: php validation query-string

我在此处有这样的查询,我将其添加到我的网址status=1&time=1030&start=Toronto&end=Boston

的末尾

我正在寻找离开来验证这些,看看状态,时间(和时间只有4个字符),开始和结束

我尝试了以下内容:

if(!isset($_GET['status'])){
   //error message and die
}

if(!isset($_GET['time']) || $_GET['time'] < 4){
   //error message and die
}

if(!isset($_GET['start'])){
   //error message and die
}

if(!isset($_GET['end'])){
   //error message and die
}

但它始终会显示第一条错误消息并死掉,即使在查询中也是如此。我该怎么解决这个问题呢?

2 个答案:

答案 0 :(得分:0)

假设您的网页为http://stackoverflow.com

确保在“?”之后添加您的查询所以你的网址应该是     像这样 :     http://stackoverflow.com?status=1&time=1030&start=Toronto&end=Boston

另一种可能性是你在页面中使用$ _POST方法。尝试将$ _GET更改为$ _REQUEST。

答案 1 :(得分:0)

尽量避免使用isset()

if(!$_GET['status'] || strlen($_GET['time'] != 4 || !$_GET['start'] || !$_GET['end']) { // error handling }