开发者

passing empty query string to php

i have question for query string

i have page wehere i pass query string like that

      abc.php?id=12

some time query sting "id" is not passed like

      abc.php

and some time value of id is empty

     abc.php?id=

and in the above 2 possibilities pages gives error.

how 开发者_JAVA技巧to fix this ?


If you check it correctly using isset() and empty() you should be able to avoid any problems.


Check if $_GET['id'] is set (using isset() ) before attempting to do anything with it.


Applying checks using the isset() language construct will supress errors and allow you to access array keys that may not exist. Here is a small function for transparent existance of a value.

$myID = getInput('id'); // Returns 'null' if ID doesn't exist but throws no PHP errors
$myID = getInput('id', 50); // Returns 50 if your ID doesn't exist

With this you can perform validation checks to make sure you have an ID

if( ($myID = getInput('id', 0)) < 1) {
     die('Invalid ID Value');
}
die("My ID is: $myID");


function getInput($key, $default = null) {
    return isset($_GET[$key]) && $_GET[$key] != null ? $_GET[$key] : $default;
}


You should always validate the incoming data before using it. In this case use isset to test if the variable exists and has a value other than null and use ctype_digit to check if the value consists only of digit characters.


check the below condition

if($_GET['id'] == '') {

 // Your Condition Here

} else if($_GET['id'] != '') {

 // Your Condition Here

} else if(isset($_GET['id'])) {

 // Your Condition Here

} 

Hope this Helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜