WebMe
homeaboutportfolioservicescontact

WebMe - Website Design & Development

previous posts
archives
 

All resources published on this blog are free to use and redistribute. All we ask is that you keep any copyright notices and links to WebMe in your code. If you find something really useful we would appreciate you placing a link on your site to ours.

 
uk web design association Independent Web Developers Portal
This is a Carbon Neutral website Follow us on Twitter
All major credit cards accepted
Web Site Design BlogSubscribe to this Blog

A better way to handle POST and GET in PHP

To handle POSTed and GET variables in PHP is a bit of a pain. You've got to check that they are actually set, that they are not null and then, read the value. We use three functions to handle this. Why three? Well, one of them (validateParam) can be used in multiple situations. Here's the code:
function validateParam($param, $default = "") {
if (!isset($param) || $param=="") {
 return $default;
}
return $param;
}

function doGet($param, $default = "") {
if (array_key_exists($param, $_GET)) {
 return validateParam($_GET[$param], $default);
}
return $default;
}
function doPost($param, $default = "") {
if (array_key_exists($param, $_POST)) {
 return validateParam($_POST[$param], $default);
}
return $default;
}
Now all you need to do is call doPost("var_name", "default") or doGet("var_name", "default") and don't have to bother about isset(), etc. Hope this helps when processing these.
Design web site media

0 Comments:

Post a Comment

<< Home

 

info@webme.co.uk

copyright About UsServicesPortfolioDemoGet QuoteContact