Answer the question
In order to leave comments, you need to log in
How to prevent a function from working outside the form page?
Greetings!
The site engine is built on MVC.
There is a page on the site that has a registration form.
The registration.html page address is generated from the Login.php controller, the registration() page function.
public function registration() {
return View::fetch($this->template_dir . 'registration');
}
https://site.ru/Login/registration.html
https://site.ru/Login/registration.html
has a registration form, which is processed by another controller: Connectemail.php, send_mail() function. <form method="post" action="/Connectemail/send_mail">
<input type="text" name="user_email" />
<input type="text" name="user_name" />
</form>
https://site.ru/Connectemail/send_mail.html?user_name=vaska&[email protected]
https://site.ru/Connectemail/send_mail.html?user_name=vaska&[email protected]
https://site.ru/Login/registration.html
? public function send_mail() {
//разрешаю работу функции только на странице контроллера Login.php
$controller=request()->controller();
if ($controller != 'Login') {
echo lang('Системная ошибка');
exit;
}
// бла-бла, код функции
}
public function send_mail() {
//разрешаю работу функции только на странице контроллера Login.php у которого я указал define("LOGIN_VERIFY_MODE", true);
if (!defined("LOGIN_VERIFY_MODE")) {
header("HTTP/1.1 404 Not Found");
echo "Not Found";
exit(1);
}
// бла-бла, код функции
}
Answer the question
In order to leave comments, you need to log in
Before building site engines using MVC, you must first read the web development primer.
And learn from it that
- PHP runs on the server. And all forms are displayed in the user's browser.
- PHP is restarted on each request. - there are no PHP functions
on the page with the form . PHP with all its functions remained on the server
- PHP knows nothing about any pages. It only sees HTTP requests. Which do not come "from the pages", but from the HTTP client. Which may be a browser, or may be any other program. But even if the browser, then again, the request does not come "from the page". And from the client's HTTP. In theory, the client can tell the server what page it requested before, but as the adjacent answer rightly states, this information is easy to fake.
- all the values of the constants set about calling one script also remained on the server , and if after that you turn to a completely different script , then he knows nothing about what the first one did.
And already independently draw a conclusion from this information that
It is impossible to prevent "the link from working on its own, outside the page with the form".
And here we move on to the next question.
Why did robots from all over the world suddenly break the chain and try to register on the site?
And because for some reason, instead of registering, the script is called send_mail.
And bots love to write letters. And as soon as they find a script that sends mail, they immediately begin to pull it, like a calf - mom's boobs.
In order for them to stop, it is enough just to call a spade a spade . And from the registration page, send the client not to the script that sends mail, but to the script that handles the registration .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question