This article mainly introduces the unified class of php input data. The example analyzes various conversion techniques for input data and has some reference value. For more information, see
This article mainly introduces the unified class of php input data. The example analyzes various conversion techniques for input data and has some reference value. For more information, see
This example describes the unified class of php input data. Share it with you for your reference. The details are as follows:
<? Phpclass cls_request {private $ getdata; // stores get data private $ postdata; // stores post data private $ requestdata; // stores request data private $ filedata; // store the file data private $ cookiedata; // store cooki static $ _ instance; // This class of instance private function _ construct () {$ this-> getdata = self: format_data ($ _ GET); $ this-> postdata = self: format_data ($ _ POST ); $ this-> requestdata = array_merge ($ this-> getdata, $ this-> postdata); $ this-> cookied Ata = self: format_data ($ _ COOKIE); $ this-> filedata = self: format_data ($ _ FILES);} // class initialization ,, returns the cls_request object public static function get_instance () {if (! (Self: $ _ instance instanceof self) {self: $ _ instance = new self ();} return self ::$ _ instance ;} // obtain the public function get_num ($ key) {if (! Isset ($ this-> getdata [$ key]) {return false;} return $ this-> to_num ($ this-> getdata [$ key]);} // obtain the public function post_num ($ key) {if (! Isset ($ this-> postdata [$ key]) {return false;} return $ this-> to_num ($ this-> postdata [$ key]);} // obtain the public function request_num ($ key) {if (! Isset ($ this-> requestdata [$ key]) {return false;} return $ this-> to_num ($ this-> requestdata [$ key]);} // obtain the public function cookie_num ($ key) {if (! Isset ($ this-> cookiedata [$ key]) {return false;} return $ this-> to_num ($ this-> cookiedata [$ key]);} // obtain the public function filedata ($ key) {return $ this-> filedata [$ key]; // return array} // GET the public function get_string ($ key, $ isfilter = true) passed by GET {if (! Isset ($ this-> getdata [$ key]) {return false;} if ($ isfilter) {return $ this-> filter_string ($ this-> getdata [$ key]);} else {return $ this-> getdata [$ key];} // obtain the POST-passed string variable public function post_string ($ key, $ isfilter = true) {if (! Isset ($ this-> postdata [$ key]) {return false;} if ($ isfilter) {return $ this-> filter_string ($ this-> postdata [$ key]);} else {return $ this-> postdata [$ key];} // obtain the public function request_string ($ key, $ isfilter = true) passed by the Request {if (! Isset ($ this-> requestdata [$ key]) {return false;} if ($ isfilter) {return $ this-> filter_string ($ this-> requestdata [$ key]);} else {return $ this-> requestdata [$ key];} // obtain the public function cookie_string ($ key, $ isfilter = true) passed by the Cookie {if (! Isset ($ this-> cookiedata [$ key]) {return false;} if ($ isfilter) {return $ this-> filter_string ($ this-> cookiedata [$ key]);} else {return $ this-> cookiedata [$ key];} // format the data private function format_data ($ data) {$ result = array (); if (! Is_array ($ data) {$ data = array ();}/** list () indicates that the variable is assigned a value using the array value. Used only for numeric index arrays. * The default value starts from 0 and goes down in order. * each () */while (list ($ key, $ value) = each ($ data )) {// not clear // process data such as checkbox if (is_array ($ value) {$ result [$ key] = $ value ;} else {// normal data $ result [$ key] = trim ($ value ); // Delete the white space at both ends of the string and other pre-defined characters }}// convert the number private function to_num ($ num) {if (is_numeric ($ num )) {return intval ($ num); // convert the variable to an integer} else {return false ;}// filter the string private function filter_string ($ data) {if ($ data = null) {retu Rn false;} if (is_array ($ data) {foreach ($ data as $ k => $ v) {$ data [$ k] = htmlspecialchars ($ v, ENT_QUOTES); // convert some predefined characters into html entities} return $ data;} else {// normal string return htmlspecialchars ($ data, ENT_QUOTES) ;}}?>
I hope this article will help you with php programming.