Jump to content

Checking for valid data question


j.smith1981

Recommended Posts

Hi there,

 

I was thinking of using this following check system to see if a user has entered a good valid value for a categories selection, but it is just is_numeric, here's my line of single code for this:

 

if(!array_key_exists('category', $_GET) || trim(is_numeric($_GET['category'])) == '')

 

Is there anything better that I can be using than is_numeric()?

 

I mean this would allow 2.22 which for the categories selection, would not be valid, I will only be allowing integer values, I mean it would potentially come up with no category selected but that wouldn't be very good would it?

 

It could be applied to a blog I am doing aswell, any guidance on such an improvement on this function would very much be appreciated,

Jeremy.

Link to comment
Share on other sites

Use is_int() to validate an integer.

 

Problem is

is_int("10")

would return false and GET is returning a string

 

Maybe

if(isset($_GET['category']) && ((int)$_GET['category'])==$_GET['category']){
  $category = (int)$_GET['category'];
//Logic is convert to int and compare to unconverted,
//as 10 == "10" is true
//ALSO 10 == "10.0" would be true
//BUT 10 == "10.2" would be false
echo "true = $category";
}

Link to comment
Share on other sites

Use is_int() to validate an integer.

 

Problem is

is_int("10")

would return false and GET is returning a string

 

Maybe

if(isset($_GET['category']) && ((int)$_GET['category'])==$_GET['category']){
}

 

Could you cast the variable as an integer and then validate using is_int() or would that return true regardless because of the cast?

Link to comment
Share on other sites

Normally i would do this

 

$category = (int)$_GET['category'];
if(!empty($category)){
//BUT if category is 0 then it will fail, but i use it main for Database ID's
}

 

Could you cast the variable as an integer and then validate using is_int() or would that return true regardless because of the cast?

 

Na, converting to an INT would make the validation pointless as it would be an int no matter what it was before hand

Link to comment
Share on other sites

Ahh I forgot about casting, thanks!

 

Kind of for a millisecond then forgot that all super global variables are always strings, but then I did use is_numeric, but casting it to an (int) is a much better idea, I don't personally see a point in comparing it, because if the user was to say put in (off the top of my head) 2.22 ah I see your point now!

 

Could just output a message if their different then say you entered some invalid data, yes will do that!

 

Sorry just thinking outloud, but yes thank you ever so much!

Link to comment
Share on other sites

In a nutshell yes, now just getting my other logic sorted but I understand what I am doing now to get an error message to the user as such if the data entered is not valid.

 

It's sometimes best I feel on occasions to go over it again and again so it firmly sticks in my head.

 

I mean the lower parts of my logic anyways it would say you haven't selected a category, but should show some error checking and that is why I was really asking to be fair.

 

Just says to me (being that I used to be a user, but got interested in Programming logic, what sort of got me into doing PHP work and the like), is that what data I did enter was valid, just no category exists for it, but to stop it from querying the database should any invalid data be attempted to be entered, if that makes any sense?

 

Sorry just my explanations need sometimes a bit of work, but I bet you get my point?

 

Thank you ever so much though!

Jeremy.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.