data validation of 24 post in php

Solutions on MaxInterview for data validation of 24 post in php by the best coders in the world

showing results for - "data validation of 24 post in php"
Ross
31 Jan 2018
1<?php
2
3class  Input {
4	static $errors = true;
5
6	static function check($arr, $on = false) {
7		if ($on === false) {
8			$on = $_REQUEST;
9		}
10		foreach ($arr as $value) {	
11			if (empty($on[$value])) {
12				self::throwError('Data is missing', 900);
13			}
14		}
15	}
16
17	static function int($val) {
18		$val = filter_var($val, FILTER_VALIDATE_INT);
19		if ($val === false) {
20			self::throwError('Invalid Integer', 901);
21		}
22		return $val;
23	}
24
25	static function str($val) {
26		if (!is_string($val)) {
27			self::throwError('Invalid String', 902);
28		}
29		$val = trim(htmlspecialchars($val));
30		return $val;
31	}
32
33	static function bool($val) {
34		$val = filter_var($val, FILTER_VALIDATE_BOOLEAN);
35		return $val;
36	}
37
38	static function email($val) {
39		$val = filter_var($val, FILTER_VALIDATE_EMAIL);
40		if ($val === false) {
41			self::throwError('Invalid Email', 903);
42		}
43		return $val;
44	}
45
46	static function url($val) {
47		$val = filter_var($val, FILTER_VALIDATE_URL);
48		if ($val === false) {
49			self::throwError('Invalid URL', 904);
50		}
51		return $val;
52	}
53
54	static function throwError($error = 'Error In Processing', $errorCode = 0) {
55		if (self::$errors === true) {
56			throw new Exception($error, $errorCode);
57		}
58	}
59}
60
Violeta
02 Jan 2020
1<?php
2	
3if ($_SERVER['REQUEST_METHOD'] === 'POST') {
4	
5		// the request method is fine
6
7} else {
8
9	exit('Invalid Request');
10
11}
12