GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Jan-2003, 01:48
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Best way to validate numbers (ids)?


I was just wondering... how do YOU validate numbers (ids) in your PHP scripts?

I used to use :

PHP Code:

<?php

$pgid = ( !is_numeric($_GET['pg']) ? 0 : $_GET['pg'] );

?>


then I settled for :
PHP Code:

<?php

$pgid = ( !is_numeric($_GET['pg']) ? 0 : intval($_GET['pg']) );

?>


now I do mostly :
PHP Code:

<?php

$pgid = ( !preg_match('/^[0-9]+$/', $_GET['pg']) ? 0 : $_GET['pg'] );

?>

  #2  
Old 20-Jan-2003, 03:55
Garth Farley Garth Farley is offline
Awaiting Email Confirmation
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
is_numeric() accepts double numbers, like "-234.45e-12" for example, but your preg emulates is_int().

I suppose it's good programming practice to use intval(), but it's not necessary. If the number is put into $pgid, and a numerical operation is applied to it, it will automatically be used as a int.

GF
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
validate form skyloon MySQL / PHP Forum 3 15-Jul-2003 09:04

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:16.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.