开发者

php address request

i have this request method:

if (!$_REQUEST['0'])  {
    echo "Address missing...";
    die();
} else {
    code
    code etc.
}

And address is: http://localhost/api.php?0=address

If I open: api.php?0=0 I should get another error which says address doesn't correct. B开发者_运维问答ut I get Address missing... In my opinion something wrong with:

if (!$_REQUEST['0']) 

Any ideas?


You're checking if $_REQUEST['0'] is false. In PHP (and many other languages), 0 stands for false. Because 0 == false, your expression results in 'true' and the echo and die() are executed.

If you are trying to test if $_REQUEST['0'] exists, you should use isset() or empty()


if(!isset($_REQUEST['0']))
{
    die("Address not provided");
}
else
{
    // code
}


  if (empty ($_REQUEST['0'])) 
  {
      echo "Address doesn't provided...";
      die();
  } else {
     code
     code etc.


if (array_key_exists('0', $_REQUEST['0']) === true) {
    echo 'exists';
}
else {
    echo 'does not';
}

Sidenote: The best check to see if a key is set in an array is array_key_exists. Opposed to isset, this function does not return false when the value is NULL.

Sidenote 2: You should specify which superglobal to use, using $_REQUEST instead of specifying $_POST or $_GET is can be the cause of vague errors to occur.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜