Answer the question
In order to leave comments, you need to log in
How to properly validate Http requests in php?
I don't want to learn how to write shit code, so I want to learn how to properly validate php requests.
The first thing that came to mind (the code in the controller):
//В начале контролера прописывается правило, которое описывает какие данные нужно ожидать и в каком виде (integer string...)
//Длинное название, лишь чтоб вы поняли что она делает. Остальные переменные не попадут в контейнер
Client::waitRequestByRule(
'POST' => array('somefield' => 'rules'),
'GET' => array('somefield' => 'rules')
); //При успешной проверке, из данных переменных будет создан контейнер
//Проверка. Получен ли запрос, соответствующий правилам
if(Cliend::requestValid()) {
$data = Client::getRequestContainer() // Получение контейнера валидированых данных
}
$data = Client::request();
$somefield = $data['GET']['somefield'];
if(Data::validate($somefield, 'rule')) return true;
Answer the question
In order to leave comments, you need to log in
From your code, it seemed to me that you were mixing request processing with responses, routing and data validation.
In general, $_COOKIE, $_POST and $_GET already form $_REQUEST . But that's not a good idea, really.
A better request handling solution would be something like PSR-7 or Symfony HttpFoundation .
For validation and routing, again, you can look at examples from Symfony: Routing , Validator .
True, judging by your code, you meant routing by validation.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question