Answer the question
In order to leave comments, you need to log in
How to properly authenticate in php?
Hello.
Actually, the question is simple: you need to make authentication on php. There is a table in the database with a login / password, there is a login page, there is a page where only logged in users should be allowed. Everything is as simple as possible, the login is only for the current session, the "Remember me" button is not needed.
My knowledge of this ends with the book “PHP for Beginners” of 2005, where it is done like this: if the login and password are correct, then a session is created with the only variable in it user_id, which stores the user ID in the database or 0 if the user not logged in. It is also used to check access. But I have a suspicion that this is not entirely correct and not very safe.
I googled and looked at the Yii, Symphony, and PHPixie sources to see how smart people do authentication, but the code is too complicated for me to understand. Too many tokens, providers, domains and more.
Answer the question
In order to leave comments, you need to log in
After googling, I looked at the sources of Yii, Symphony and PHPixie to find out how smart people do authentication
Try using the example from Symfony but with xdebug enabled. Go through the authentication process from the beginning of the request to the return of the response. In fact, xdebug should be used in principle for situations where you don't know what's going on in the code))
PHP for Beginners" 2005, where it is done like this: if the login and password are correct, then a session is created with the only variable in it user_id, which stores the user ID in the database or 0 if the user is not logged in. It is also used to check access. But I have a suspicion that this is not entirely correct and not very safe.
I think that Sergey Protko chewed everything up enough, but I will give you a safe and simple code to make it clearer:
// Готовим запрос к базе данных ...
$pdo->$main->pdo_prepare("SELECT `*` FROM TABLE `xxx` WHERE login=`?` AND password= `?`");
// Готовим плейсхолдеры и формируем пейлоад.
$pdo->$main->pdo_append(array(html_real_escape($_REQUEST["login"]),html_real_escape($_REQUEST["password"])));
// Выполняем запрос в базу ...
$pdo->$main->pdo_execute();
// Читаем запрос из базы в виде ассоциативного массива...
$pdo->$main->fetch_row();
if($row["login"] == $_REQUEST["login"]) {
echo "Пользователь прошёл проверку, доступ разрешён.";
} esle {
echo " Пользователь не прошёл проверку. ";
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question