Dillion & Amanda 4 Lif Posted June 5, 2010 Posted June 5, 2010 function check_int($check){ if(preg_match("#\D#is", $check)){ echo 'Error text here.'; exit; } if(!ctype_digit($check) || !is_numeric($check)){ echo 'Error text here.'; exit; } } Just thought of it. Basically first it checks if it matches anything except a number. Then it checks if its a valid number. Please tell me if its useful or something, not sure but it works. Quote
rulerofzu Posted June 5, 2010 Posted June 5, 2010 I see your logic. But not really for mccodes. In order for it to work you would have to implement it through the code and if your doing that just change the code to make sure its checking correctly anyway. Also its my understanding. ctype_digit will return false if its not a number anyway. Quote
Jordan Palmer Posted June 5, 2010 Posted June 5, 2010 I really don't see the point in this? If it's not a number don't process the script, If they've entered text where it says number - they know what there doing wrong :D Good try I think Quote
Dillion & Amanda 4 Lif Posted June 5, 2010 Author Posted June 5, 2010 I figured it'd work to help prevent others that dont have .htaccess that block xss, help get rid of some of it. like i said though wasnt sure if itd be useful or not Quote
Zeggy Posted June 5, 2010 Posted June 5, 2010 If you take your code and put it into pseudo-code, here's what it is: function check_int: if (not a number) exit; if (not a number or not a number) exit; You're using a lot of redundant code. There is no need for both conditionals, both perform (almost) the same tests. In your second conditional, if the variable passes the ctype_digit test, then it will pass the is_numeric test for sure (since ctype_digit is a stricter test than is_numeric), making the second part of the if also redundant. Your function will give the same results as this: function check_int($check){ if(!ctype_digit($check) ){ echo 'Error text here.'; exit; } } Also, you don't want to be using exit in a function, use return, otherwise you are breaking the flow of the page. Quote
Dillion & Amanda 4 Lif Posted June 5, 2010 Author Posted June 5, 2010 Well if u add $h->endpage() above exit for mccodes itll keep the right design. Quote
Zeggy Posted June 5, 2010 Posted June 5, 2010 Well if u add $h->endpage() above exit for mccodes itll keep the right design. I think you missed the point of my post. But that's still bad coding practice. Why would you need to stop the script if a variable isn't an integer? Surely you would want to let the script handle the error, and display its own error message or handle it some other way. All your function does is check if a variable is an integer, in very few situations will you ever want the entire page to stop working just because a variable is a wrong type. I would suggest returning true or false, if you extend the function in a useful way. At the moment it's just a slower alias of ctype_digit(). Quote
Zero-Affect Posted June 5, 2010 Posted June 5, 2010 I kind of just skipped over what people said lol but doesn't ctype_digit and is_numeric give different results? also ctype_digit won't result in +123.00 being numeric but is_numeric will... im sure there's a negative integer also. Quote
Djkanna Posted June 5, 2010 Posted June 5, 2010 if(filter_var($var, FILTER_VALIDATE_INT)) { exit; } else { $id = abs(filter_var($var, FILTER_SANITIZE_NUMBER_INT)); } I use something to that effect. Quote
rulerofzu Posted June 5, 2010 Posted June 5, 2010 if(filter_var($var, FILTER_VALIDATE_INT)) { exit; } else { $id = abs(filter_var($var, FILTER_SANITIZE_NUMBER_INT)); } I use something to that effect. Your so PHP 5 :thumbsup: Quote
Djkanna Posted June 5, 2010 Posted June 5, 2010 if(filter_var($var, FILTER_VALIDATE_INT)) { exit; } else { $id = abs(filter_var($var, FILTER_SANITIZE_NUMBER_INT)); } I use something to that effect. Your so PHP 5 :thumbsup: Haha :thumbsup: Quote
rulerofzu Posted June 6, 2010 Posted June 6, 2010 Id say your special Crim...in your own special way! lol Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.