Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > PHP question

PHP question
Thread Tools
Arkham_c
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Nov 18, 2003, 03:00 PM
 
I'm writing a medium sized object-oriented php app. I want to define a method that returns an object. That object is constructed via the return of a database query like

"SELECT * FROM PROPERTY WHERE PROPERTY_ID = 5"

In the example, 5 is the property ID, and it will be passed in via query string.

My concern is, how do I verify in PHP that the value passed in via query string is in fact a number?

I am concerned that someone could pass in "; DROP TABLE PROPERTY" or something equally malicious, and I want to check the input before putting it in my query.
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
schk
Dedicated MacNNer
Join Date: Jan 2003
Status: Offline
Reply With Quote
Nov 18, 2003, 03:15 PM
 
You can always use $variable = (int)$variable to make sure the value is an integer. If it is a text string, it will be converted to 0.
     
redJag
Senior User
Join Date: Dec 2002
Status: Offline
Reply With Quote
Nov 18, 2003, 03:34 PM
 
if(is_int($property_id)){
//execute
}
else{
echo "Error: non-numerical property ID passed";
}
Travis Sanderson
     
Arkham_c  (op)
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Nov 18, 2003, 03:44 PM
 
Originally posted by redJag:
if(is_int($property_id)){
//execute
}
else{
echo "Error: non-numerical property ID passed";
}
is_int -- just what I needed, thanks!

For anyone in the future, the function is documented here:

http://www.php.net/is_int
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Arkham_c  (op)
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Nov 18, 2003, 04:24 PM
 
Ok, so is_int is NOT what I needed. This code:

[php]
$somenum = "5544";
if ( is_int($somenum) )
{
echo "an int";
}
else
{
echo "not an int";
}
[/php]

will print "not an int". I found this online:

[php]
function isAnInt($x)
{
return (is_numeric($x) ? intval($x) == $x : false);
}
[/php]
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Simon Mundy
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Nov 18, 2003, 06:49 PM
 
...or you can use

"SELECT * FROM PROPERTY WHERE PROPERTY_ID = ".(INT) $property_id

...so it simply forces it to the correct type without needing conditionals.
Computer thez nohhh...
     
redJag
Senior User
Join Date: Dec 2002
Status: Offline
Reply With Quote
Nov 18, 2003, 06:58 PM
 
Originally posted by Arkham_c:
Ok, so is_int is NOT what I needed. This code:

[php]
$somenum = "5544";
if ( is_int($somenum) )
{
echo "an int";
}
else
{
echo "not an int";
}
[/php]

will print "not an int".
That's because "5544" is a string, not an integer If you type $somenum = 5544; then it will work
Travis Sanderson
     
Arkham_c  (op)
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Nov 18, 2003, 09:18 PM
 
Originally posted by redJag:
That's because "5544" is a string, not an integer If you type $somenum = 5544; then it will work
Yes, but all form data comes in as a string. That's the concern.
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
redJag
Senior User
Join Date: Dec 2002
Status: Offline
Reply With Quote
Nov 18, 2003, 10:38 PM
 
Originally posted by Arkham_c:
Yes, but all form data comes in as a string. That's the concern.
OK Form was never mentioned Glad you got things as they should be!
Travis Sanderson
     
clam2000
Dedicated MacNNer
Join Date: Aug 2002
Status: Offline
Reply With Quote
Nov 18, 2003, 11:06 PM
 
alternately you can use regular expressions
[php]
if(ereg('^[[:digit:]]+$', $property_id))) {
//execute
} else {
die("not a number");
}
[/php]
regular expresion used:
^ start of line
[:digit:] a digit, 0-9
+ one or more of whatever is within the preceding brackets
$ end of line


--will
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 12:54 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,