I do authorization on the site. When a user enters data, they are sent to the server, processed and returned a message. But the problem is that after the user clicks the "Authorization" button, I have a double redirect (it seems so called), that is, the first time the redirect occurs after the user sends the data, and the second time, it does I have written a function. Let me show you how this whole thing works for me:
Function in the model.php file
function authorization () {
$ link = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die (mysqli_error ($ link)); ``
$ responseArray = [];
$ access = true;
$ login = clear ($ _ POST ['login']);
$ password = clear ($ _ POST ['password']);
$ login_hash = md5 (md5 (time () + time () * rand (2,10)));
if (empty (login))
$ responseArray ['error'] ['login'] = 'Login cannot be allowed';
if (empty ($ password))
$ responseArray ['error'] ['password'] = 'The password cannot be empty';
if ($ responseArray ['error']) $ access = false;
if ($ access) {
$ select_query = "SELECT id, user, password, login_hash, user_ip
FROM user_credentials
WHERE user = '$ login'
AND activated = 1 ";
$ select_result = mysqli_query ($ link, $ select_query)
or die (mysqli_error ($ link));
if (mysqli_num_rows ($ select_result)> 0) {
$ select_arr = mysqli_fetch_assoc ($ select_result);
$ password_correct = $ select_arr ['password'];
if (! password_verify ($ password, $ password_correct)) {
$ responseArray ['error'] ['denied'] = 'Hibny login abo password';
$ _SESSION ['auth'] ['login'] = $ login;
} else {
$ query_insert = "UPDATE user_credentials
SET login_hash = '$ login_hash'
WHERE user_credentials.user = '$ login' ";
mysqli_query ($ link, $ query_insert) or die (mysqli_error ($ link));
setcookie ('auth', $ login_hash, time () + 3600 * 2, '/ admin /');
setcookie ('login', md5 ($ login), time () + 3600 * 2, '/ admin /');
$ responseArray ['success'] ['auth'] = 'You are in the system';
}
} else {
$ responseArray ['error'] ['denied'] = 'There is no such answer ”;
$ _SESSION ['auth'] ['login'] = $ login;
}
}
return $ responseArray;
}
After that, the data is sent to the server, and $responseArray should return with the desired message.
But in the file controller.php , where I call the function at the right moment, I wrote the following:
if ($ _ SERVER ['REQUEST_METHOD'] == 'POST' && isset ($ _ POST ['authorization'])) {
$ responseArray = authorization ();
redirect ();
}
redirect(); - I have a separate f-tion that overloads the page. This is what I do to prevent the form from being sent again in case the user reloads the page. Here is the f-tion itself:
function redirect () {
$ redirect = isset ($ _ SERVER ['HTTP_REFERER'])? $ _SERVER ['HTTP_REFERER']: VIEW;
header ("Location: $ redirect");
exit;
}
I get messages in the file authorization.php
What I need? After the user presses the authorization button to display messages from the responseArray array after this redirect(); . Without it, everything works fine, is displayed. But as soon as I redirect, the array disappears. Advise me how to solve this issue. Thank.