Long time ago I built a class for these needs. In fact the half of the code is *stolen* from implemenation of my colleague. I just made it OOP. The class in pi-validate.class.php.txt, the definitions of the checked fields is in pi-validate.defs.php Sample use : $validate_result = CValidate::validate_validate_data(VALIDATE_NEW_USER, $user_data); if ($validate_result[0] != GENERAL_OPERATION_SUCCESSFUL){ return array(VALIDATE_FAIL, VALIDATE_FAIL_MESSAGE, $validate_result[2]); }/* if */ It is kindly complicated example but hope you get it. For every new page just define the variables you expect to come and rules for them and nothing more is needed except to place a code like that above to check the validity. In case of error $validate_result will contain information for the error. Regards, Andrey ----- Original Message ----- From: "Addison Ellis" <addison@bellsouth.net> To: <php-db@lists.php.net> Sent: Wednesday, January 22, 2003 9:24 AM Subject: foreach($HTTP_POST_VARS as $key => $value) > hello, > i do appreciate your time... > below the code following i don't know where to go. do i need > to list each value individually or can i do something with > $HTTP_POST_VARS or am i even headed in the right direction? i am > trying to point all my forms to this .php file to check for errors, > and if they are ok, insert the submitted form values into a table in > a database. thank you. addison ellis > foreach($HTTP_POST_VARS as $key => $value) > { > if ($key != "") > { > if ($value == "") > { > $message_new = "<font face=verdana size=1 > color=red>Required information missing.\n > Please try again.</font>"; > } > } > if (ereg("{name)",$key)) > { > if (!ereg("^[A-Za-z' -]{1,50}$",$key)) > { > $message_new = "<font face=verdana size=1 color=red>Some > information not processing.\n > Please try again.</font>"; > } > } > $$key = strip_tags(trim($value)); > } > if (!ereg("^[0-9)(xX -]{7,20}$",$phone)) > { > $message_new = "<font face=verdana size=1 color=red>Not a > valid phone number.\n > Please try again.</font>"; > } > if (!ereg("^.+@.+\\..+$",$email)) > { > $message_new = "<font face=verdana size=1 color=red>Not a > valid email address.\n > Please try again.</font>"; > } > else > if ($message == "") > { > $query = "insert into ad_columns set > -- > Addison Ellis > small independent publishing co. > 114 B 29th Avenue North > Nashville, TN 37203 > (615) 321-1791 > addison@bellsouth.net > info@smipco.com > subsidiaries of small independent publishing co. > info@gloabaldog.com > info@momandpocentral.com
<?php /** Definition file - defintions of various data. This module is part from PHPIntra system and is licensed under GPL. @author Andrey Hristov <andrey@hristov.com> @version $Id: pi-validate.defs.php,v 1.3 2002/07/09 07:30:22 nobody Exp $ @package PHPIntra @module pi_validate_defs */ /** @const VALIDATE_NEW_RULE . */ define('VALIDATE_NEW_RULE', 'new_rule_defs'); /** @const VALIDATE_UPDATE_RULE . */ define('VALIDATE_UPDATE_RULE', 'update_rule_defs'); /** @const VALIDATE_NEW_USER . */ define('VALIDATE_NEW_USER', 'new_user_defs'); /** @const VALIDATE_CHANGE_USER_DATA . */ define('VALIDATE_CHANGE_USER_DATA', 'change_user_defs'); /** @const VALIDATE_CREATE_TASK_DATA . */ define('VALIDATE_CREATE_TASK_DATA', 'create_task'); /** @const VALIDATE_UPDATE_TASK_DATA . */ define('VALIDATE_UPDATE_TASK_DATA', 'update_task'); /** @const VALIDATE_CREATE_WSESSION_DATA . */ define('VALIDATE_CREATE_WSESSION_DATA', 'create_wsession'); /* T - Text H - Hidden P - Password A - text Area F - File upload L - List (select) M - Multiple Selection C - Checkbox */ define('VALIDATE_TYPE_INT',1); define('VALIDATE_TYPE_FLOAT',2); define('VALIDATE_TYPE_ARRAY',3); define('VALIDATE_TYPE_STRING',4); define('VALIDATE_SELECT_NONE','none'); $VALIDATE_DEFINITIONS = array( VALIDATE_UPDATE_RULE => array( 'old_schedule_id' => array( 'type' => 'T', 'pattern' => '/^\d+$/', 'optional' => 0, 'maxlen' => 10, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), 'old_start_time' => array( 'type' => 'T', 'pattern' => '/^(?:(?:(?:0|1|)[\d]{1})|(?:2[0-3]{1}))(?:\:[\d]{0,2}){0,2}$/', 'optional' => 0, 'maxlen' => 8, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_STRING, ), 'schedule_id' => array( 'type' => 'T', 'pattern' => '/^\d+$/', 'optional' => 0, 'maxlen' => 10, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), 'date_mask' => array( 'type' => 'T', 'pattern' => '/^(?:[AaBbCdDegGhjmntuUVWwY%]{2})(?:[\:\040\|\/\_\-]*[AaBbCdDegGhjmntuUVWwY%]{2})*$/', 'optional' => 1, 'maxlen' => 20, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_STRING, ), 'date_mask_parsed'=> array( 'type' => 'T', 'pattern' => '/^(?:[\w\d%]+)(?:[\:\040\|\/]*[\w\d%]+)*$/', 'optional' => 0, 'maxlen' => 40, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_STRING, ), 'start_time' => array( 'type' => 'T', 'pattern' => '/^(?:(?:(?:0|1|)[\d]{1})|(?:2[0-3]{1}))(?:\:[\d]{0,2}){0,2}$/', 'optional' => 0, 'maxlen' => 8, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_STRING, ), 'duration' => array( 'type' => 'T', 'pattern' => '/^(?:(?:(?:[0-8][\d]{4})|(?:[\d]{1,4})))$/', 'optional' => 0, 'maxlen' => 5, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), ), VALIDATE_NEW_RULE => array( 'date_mask' => array( 'type' => 'T', 'pattern' => '/^(?:[AaBbCdDegGhjmntuUVWwY%]{2})(?:[\:\040\|\/\_\-]*[AaBbCdDegGhjmntuUVWwY%]{2})*$/', 'optional' => 1, 'maxlen' => 20, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_STRING, ), 'date_mask_parsed'=> array( 'type' => 'T', 'pattern' => '/^(?:[\w\d%]+)(?:[\:\040\|\/]*[\w\d%]+)*$/', 'optional' => 0, 'maxlen' => 40, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_STRING, ), 'start_time' => array( 'type' => 'T', 'pattern' => '/^(?:(?:(?:0|1|)[\d]{1})|(?:2[0-3]{1}))(?:\:[\d]{0,2}){0,2}$/', 'optional' => 0, 'maxlen' => 8, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_STRING, ), 'duration' => array( 'type' => 'T', 'pattern' => '/^(?:(?:(?:[0-8][\d]{4})|(?:[\d]{1,4})))$/', 'optional' => 0, 'maxlen' => 5, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), ), VALIDATE_NEW_USER => array( 'branch_id' => array( 'type' => 'T', 'pattern' => '/^\d+$/', 'optional' => 0, 'maxlen' => 5, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), 'fname' => array( 'type' => 'T', 'pattern' => "/^[a-zA-Z \.\'\-]{1,30}$/", 'optional' => 0, 'maxlen' => 30, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_STRING, ), 'mname' => array( 'type' => 'T', 'pattern' => "/^[a-zA-Z \.\'\-]{1,30}$/", 'optional' => 0, 'maxlen' => 30, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_STRING, ), 'lname' => array( 'type' => 'T', 'pattern' => "/^[a-zA-Z \.\'\-]{1,30}$/", 'optional' => 0, 'maxlen' => 30, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_STRING, ), 'phone_work' => array( 'type' => 'T', 'pattern' => '/^[+]?([(]?[\d]{1,7}[)]?[\-\040]?)+$/', 'optional' => 0, 'maxlen' => 25, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_STRING, ), 'phone_mobile' => array( 'type' => 'T', 'pattern' => '/^[+]?([(]?[\d]{1,7}[)]?[\-\040]?)+$/', 'optional' => 1, 'maxlen' => 25, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_STRING, ), 'phone_home' => array( 'type' => 'T', 'pattern' => '/^[+]?([(]?[\d]{1,25}[)]?[\-\040]?)+$/', 'optional' => 0, 'maxlen' => 25, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_STRING, ), 'email_corporate'=> array( 'type' => 'T', 'pattern' => '/^[\w\d\_](?:[\w\d\_\-\.]*[\w\d]*)*\@(?:[\w\d](?:[\w\d\_\-]*?[\w\d]+)*\.)+(?:[\w]{2,4})$/i', 'optional' => 0, 'maxlen' => 150, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_STRING, ), 'email_external'=> array( 'type' => 'T', 'pattern' => '/^[\w\d\_](?:[\w\d\_\-\.]*[\w\d]*)*\@(?:[\w\d](?:[\w\d\_\-]*?[\w\d]+)*\.)+(?:[\w]{2,4})$/i', 'optional' => 0, 'maxlen' => 150, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_STRING, ), 'login' => array( 'type' => 'T', 'pattern' => '/^[0-9a-zA-Z_\-]{3,15}$/', 'optional' => 0, 'maxlen' => 15, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_STRING, ), 'passwd' => array( 'type' => 'P', 'pattern' => '/^[0-9a-zA-Z]{4,128}$/', 'optional' => 0, 'maxlen' => 128, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_STRING, ), 'old_passwd' => array( 'type' => 'P', 'pattern' => '/^[0-9a-zA-Z]{4,128}$/', 'optional' => 1, 'maxlen' => 128, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_STRING, ), 'timezone' => array( 'type' => 'T', 'pattern' => '/^(?:\-|\+|)(?:(?:0)?\d|1[0-2])$/', 'optional' => 0, 'maxlen' => 3, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), 'user_mode' => array( 'type' => 'T', 'pattern' => '/^[0-4]$/', 'optional' => 0, 'maxlen' => 1, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), ), VALIDATE_CREATE_TASK_DATA => array( 'task_id' => array( 'type' => 'T', 'pattern' => '/^(\d{1,10})$/', 'optional' => 0, 'maxlen' => 10, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), 'ptask_id' => array( 'type' => 'T', 'pattern' => '/^(\d{1,10}|NULL)$/', 'optional' => 0, 'maxlen' => 10, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_STRING, ), 'task_name' => array( 'type' => 'T', 'pattern' => "/^[\w\d\040\-\_\.\$\%\@\!\&\(\)\+\=\[\]\;\\\"\']{3,100}$/", 'optional' => 0, 'maxlen' => 100, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_STRING, ), 'status_id' => array( 'type' => 'T', 'pattern' => '/^('.DB_TASK_STATUS_OPEN.'|'.DB_TASK_STATUS_SUSPENDED.'|'.DB_TASK_STATUS_CLOSED.')$/', 'optional' => 1, 'maxlen' => 1, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), 'managers_group' => array( 'type' => 'T', 'pattern' => '/^[0-9a-zA-Z_\-]{3,15}(\:[0-9a-zA-Z_\-]{3,15})*$/', 'optional' => 0, 'maxlen' => 100, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), 'workers_group' => array( 'type' => 'T', 'pattern' => '/^[0-9a-zA-Z_\-]{3,15}(\:[0-9a-zA-Z_\-]{3,15})*$/', 'optional' => 0, 'maxlen' => 100, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), 'due_date' => array( 'type' => 'T', 'pattern' => '/^\d{1,12}$/', 'optional' => 0, 'maxlen' => 12, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), 'time_limit' => array( 'type' => 'T', 'pattern' => '/^\d{1,30}$/', 'optional' => 0, 'maxlen' => 30, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), ), VALIDATE_CREATE_WSESSION_DATA => array( 'ws_id' => array( 'type' => 'T', 'pattern' => '/^(\d{1,10})$/', 'optional' => 1, 'maxlen' => 10, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), 'task_id' => array( 'type' => 'T', 'pattern' => '/^(\d{1,10})$/', 'optional' => 0, 'maxlen' => 10, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), 'hour_from' => array( 'type' => 'T', 'pattern' => '/^((0|)\d|1\d|2[0-3])$/', 'optional' => 0, 'maxlen' => 2, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), 'hour_to' => array( 'type' => 'T', 'pattern' => '/^((0|)\d|1\d|2[0-3])$/', 'optional' => 0, 'maxlen' => 2, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), 'minute_from' => array( 'type' => 'T', 'pattern' => '/^((0|)\d|[1-5]\d)$/', 'optional' => 0, 'maxlen' => 2, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), 'minute_to' => array( 'type' => 'T', 'pattern' => '/^((0|)\d|[1-5]\d)$/', 'optional' => 0, 'maxlen' => 2, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), 'second_from' => array( 'type' => 'T', 'pattern' => '/^((0|)\d|[1-5]\d)$/', 'optional' => 0, 'maxlen' => 2, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), 'second_to' => array( 'type' => 'T', 'pattern' => '/^((0|)\d|[1-5]\d)$/', 'optional' => 0, 'maxlen' => 2, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), 'month_from' => array( 'type' => 'T', 'pattern' => '/^((0|)\d|1[0-2])$/', 'optional' => 0, 'maxlen' => 2, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), 'month_to' => array( 'type' => 'T', 'pattern' => '/^((0|)\d|1[0-2])$/', 'optional' => 0, 'maxlen' => 2, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), 'day_from' => array( 'type' => 'T', 'pattern' => '/^((0|)\d|[1-2]\d|3[0-1])$/', 'optional' => 0, 'maxlen' => 2, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), 'day_to' => array( 'type' => 'T', 'pattern' => '/^((0|)\d|[1-2]\d|3[0-1])$/', 'optional' => 0, 'maxlen' => 2, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), 'year_from' => array( 'type' => 'T', 'pattern' => '/^(19\d{2}|2\d{3})$/', 'optional' => 0, 'maxlen' => 4, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), 'year_from' => array( 'type' => 'T', 'pattern' => '/^(19\d{2}|2\d{3})$/', 'optional' => 0, 'maxlen' => 4, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), 'comment' => array( 'type' => 'T', 'pattern' => '/.*/', 'optional' => 1, 'maxlen' => 65536, 'allowtags' => 1, 'lang_type' => VALIDATE_TYPE_STRING, ), 'deleted' => array( 'type' => 'T', 'pattern' => '/^(?:0|1)$/', 'optional' => 0, 'maxlen' => 1, 'allowtags' => 0, 'lang_type' => VALIDATE_TYPE_INT, ), ), ); $VALIDATE_DEFINITIONS[VALIDATE_CHANGE_USER_DATA] = &$VALIDATE_DEFINITIONS[VALIDATE_NEW_USER]; $VALIDATE_DEFINITIONS[VALIDATE_UPDATE_TASK_DATA] = &$VALIDATE_DEFINITIONS[VALIDATE_CREATE_TASK_DATA]; ?>
<?php /** Remote service for data validation. Uses SOAP. This module is part from PHPIntra system and is licensed under GPL. @author Andrey Hristov <andrey@hristov.com> @version $Id: pi-validate-remote.class.php,v 1.1.1.1 2002/06/09 22:51:06 root Exp $ @package PHPIntra @module validate_class */ require_once REMOTE_LIB.'pi-status_msgs.defs.php'; require_once REMOTE_DEFS.'pi-validate.defs.php'; /** * CValidate * * @author Andrey Hristov <andrey@hristov.com> * @version v 0.2 * @access public */ class CValidate{ /* T - Text H - Hidden P - Password A - text Area F - File upload L - List (select) M - Multiple Selection C - Checkbox */ /** * Validates data. * * @access public * @return array $ret_ar */ function validate_validate_data($service_name, $data){ global $VALIDATE_DEFINITIONS; $ok = array(); $err = array(); if (!isset($VALIDATE_DEFINITIONS[$service_name])){ return array(VALIDATE_NO_SUCH_SERVICE, VALIDATE_NO_SUCH_SERVICE_MESSAGE); } $RULES = $VALIDATE_DEFINITIONS[$service_name]; $all_ok = VALIDATE_ALL_FIELDS_OK; foreach ($RULES as $key => $key_def){ $ok[$key] = VALIDATE_FIELD_WRONG; $err[$key] = array(); if (!isset($RULES[$key])){ $ok[$key] = VALIDATE_FIELD_WRONG; $err[$key] = array(VALIDATE_REQUIRED_FIELD, VALIDATE_REQUIRED_FIELD_MESSAGE); continue; } if(isset($data[$key])){ if ($RULES[$key]['optional'] == 1){ switch ($RULES[$key]['type']){ case 'M':{ if(count($data[$key]) == 0){ $ok[$key] = VALIDATE_FIELD_OK; } break; } case 'C': case 'T': case 'H': case 'P': case 'A': case 'F':{ if($data[$key] == ''){ $ok[$key] = VALIDATE_FIELD_OK; } break; } case 'L':{ if($data[$key] == VALIDATE_SELECT_NONE){ $ok[$key] = VALIDATE_FIELD_OK; } break; } }/* switch */ if($ok[$key] == VALIDATE_FIELD_OK){ continue; } }/* if optional */ switch ($RULES[$key]['type']){ case 'M':{ if(count($data[$key]) == 0){ $err[$key] = array(VALIDATE_REQUIRED_FIELD, VALIDATE_REQUIRED_FIELD_MESSAGE); } break; } case 'C':{ if($data[$key] == ''){ $err[$key] = array(VALIDATE_REQUIRED_FIELD, VALIDATE_REQUIRED_FIELD_MESSAGE);; } break; } case 'T': case 'H': case 'P': case 'A': case 'F':{ if($data[$key] === ''){ /* === instead of == because 0 -> '' in type juggling when == */ $err[$key] = array(VALIDATE_REQUIRED_FIELD, VALIDATE_REQUIRED_FIELD_MESSAGE); }else{ if(strlen($data[$key]) > $RULES[$key]['maxlen']){ $err[$key] = array(VALIDATE_TOO_LONG_FIELD, VALIDATE_TOO_LONG_FIELD_MESSAGE); }else{ if ($RULES[$key]['type'] != 'F' && $RULES[$key]['type'] != 'P' && $RULES[$key]['type'] != 'C' && !$RULES[$key]['allowtags'] && preg_match('/\<.*\>/', $data[$key])){ $err[$key] = array(VALIDATE_HTML_NOT_ALLOWED, VALIDATE_HTML_NOT_ALLOWED_MESSAGE); }/* if */ }/* if */ }/* if */ break; }/* case */ case 'L':{ if($data[$key] == VALIDATE_SELECT_NONE){ $err[$key] = array(VALIDATE_REQUIRED_FIELD, VALIDATE_REQUIRED_FIELD_MESSAGE); } break; }/* case */ }/* switch ($RULES[$key]['type']) */ if(count($err[$key])){ $all_ok = VALIDATE_SOME_FIELDS_NOT_OK; continue; }/* if */ switch ($RULES[$key]['type']){ case 'M':{ $ok[$key] = VALIDATE_FIELD_OK; for($elem_cnter_multi = 0; $elem_cnter_multi < count($data[$key]); $elem_cnter_multi++){ if(isset($data[$key][$elem_cnter_multi])){ if( !preg_match($RULES[$key]['pattern'], $data[$key][$elem_cnter_multi]) ){ $all_ok = VALIDATE_SOME_FIELDS_NOT_OK; $ok[$key] = VALIDATE_FIELD_WRONG; $err[$key] = array(VALIDATE_VALUE_NOT_VALID, VALIDATE_VALUE_NOT_VALID_MESSAGE); break; }/* if */ }else{ $data[$key][$elem_cnter_multi] = ''; }/* if */ }/* for */ break; } default:{ if(preg_match($RULES[$key]['pattern'], $data[$key])) $ok[$key] = VALIDATE_FIELD_OK; else{ $all_ok = VALIDATE_SOME_FIELDS_NOT_OK; $err[$key] = array(VALIDATE_VALUE_NOT_VALID, VALIDATE_VALUE_NOT_VALID_MESSAGE); } break; } }/* switch*/ }else{ if($RULES[$key]['type'] != 'M'){ $data[$key] = ''; }else{ $data[$key] = array(); } if($RULES[$key]['optional']){ $ok[$key] = VALIDATE_FIELD_OK; }else{ $all_ok = VALIDATE_SOME_FIELDS_NOT_OK; $err[$key] = array(VALIDATE_REQUIRED_FIELD, VALIDATE_REQUIRED_FIELD_MESSAGE); } }/* if */ }/* foreach */ if ($all_ok == VALIDATE_ALL_FIELDS_OK){ return array(GENERAL_OPERATION_SUCCESSFUL, GENERAL_OPERATION_SUCCESSFUL_MESSAGE); }else{ return array(GENERAL_OPERATION_FAILED, GENERAL_OPERATION_FAILED_MESSAGE, $err); } }/* validate_validate_data */ }/* CValidate */ ?>
-- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php