March 23, 201016 yr If I wanted to run a particular function or other bit of code, such as stripslashes used here, on all data coming in from a form before I proceed to check individual variables in the usual way, would this be a good way of doing it? function get($key, $source=$_POST) ( $item = $source[$key]; return stripslashes($item); } Is this code correct? Safe? Reliable? Or not. I have not had a chance to test it yet.
March 23, 201016 yr If I wanted to run a particular function or other bit of code, such as stripslashes used here, on all data coming in from a form before I proceed to check individual variables in the usual way, would this be a good way of doing it? function get($key, $source=$_POST) ( $item = $source[$key]; return stripslashes($item); } Is this code correct? Safe? Reliable? Or not. I have not had a chance to test it yet. Do something like this function protect($string){ $string = strip_tags($string); $string = stripslashes($string); return $string; } The use it on a post variable like this $name = (isset($_POST['name'])) ? protect($_POST['name']) : FALSE;
March 23, 201016 yr Author Thanks for the reply, but I'm not going to run a function across 15 post variables separately. That's exactly the kind of code bloat that I want to avoid if at all possible. That's the point of my question, basically: anything that streamlines the process is welcome.
March 23, 201016 yr while(list($key,$value) = each($_POST)) { //do what ever you want here for example $$key = stripslashes(strip_tags(trim($value))); } This way loops through all posted vaiables. Thanks Nathan Attain Design Edited March 23, 201016 yr by nathan@attaindesign.co.uk
March 23, 201016 yr Thanks for the reply, but I'm not going to run a function across 15 post variables separately. That's exactly the kind of code bloat that I want to avoid if at all possible. That's the point of my question, basically: anything that streamlines the process is welcome. As far as i know it thats the only way to do it is put the function around each variable i could be wrong though i personally don't use anyother way and its not really code bloat its one function you just put around the variable you wanna protect when you create that variable way easier then doing it like this $name = stripslashes(strip_tags(trim($_POST['name'])));
March 23, 201016 yr Author @ Nathan That looks good. Thanks. I'll try it when I can. @ webdesigner93 Well, we can agree to differ about what constitutes bloat but I definitely don't want to write out 15 isset statements per your example if I can loop through all data in one function instead and avoid too much repetition. But thanks for your input anyway: it is always appreciated.
March 23, 201016 yr @ Nathan That looks good. Thanks. I'll try it when I can. @ webdesigner93 Well, we can agree to differ about what constitutes bloat but I definitely don't want to write out 15 isset statements per your example if I can loop through all data in one function instead and avoid too much repetition. But thanks for your input anyway: it is always appreciated. Well the isset for the variables prevents the unidentified index error otherwise your just going to be filling the error log with useless errors
March 23, 201016 yr Author @ Jay How would I get all the post variables into an array? I haven't done that before.
March 24, 201016 yr Author @ Rallport Please elaborate on what your last comment is supposed to mean.
March 25, 201016 yr Author Yes, it did come across as a bit offensive. I didn't just jump into PHP without first reading books, website tutorials, and parts of the manual. But what immediately makes perfect sense to some will be a difficult concept for others. My knowledge of it has been slowly getting there over the past year, on and off when I have time for it. My specialisms are IA and UX rather than server-side. No worries, though.
Create an account or sign in to comment