<?php $dbc = mysqli_connect('localhost', 'kut', '', 'lic'); if(!isset($_COOKIE['user_id'])) { if(isset($_POST['submit'])) { $user_username = mysqli_real_escape_string($dbc, trim($_POST['username'])); $user_password = mysqli_real_escape_string($dbc, trim($_POST['password'])); if(!empty($user_username) && !empty($user_password)) { $query = "SELECT `user_id` , `username` FROM `signup` WHERE username = '$user_username' AND password = SHA('$user_password')"; $data = mysqli_query($dbc,$query); if(mysqli_num_rows($data) == 1) { $row = mysqli_fetch_assoc($data); setcookie('user_id', $row['user_id'], time() + (60*60*24*30)); setcookie('username', $row['username'], time() + (60*60*24*30)); $home_url = 'http://' . $_SERVER['HTTP_HOST']; header('Location: '. $home_url); } else { echo 'Извините, вы должны ввести правильные имя пользователя и пароль'; } } else { echo 'Извините вы должны заполнить поля правильно'; } } } ?> 

After entering the correct password and user login (in any case), it just pops out - 'Sorry, you must enter the correct username and password'. How to solve it?

  • Oh, nostalgia! How many forums were "hacked" in the 90s due to such "authorization". you substitute any user in your cookie - and go ahead - Ipatiev
  • If you translate into Russian, the beginning of your code reads as: Connecting to the database. If there is no cookie, an error appears. If there is no value in the $ _POST array with the name submit ... Does the form contain a name for submit? Right? - Alexey

1 answer 1

Most likely you do not have a connection to the database, check that it happens, your $ user_username and $ user_password are null.

PS use the session mechanism, you have an unsafe code, type in Google / Yandex "php session authorization example"

  • This answer contradicts the conditions described in the question. - Ipatiev
  • What is it contrary to? in post script? The answer contains a very specific reason why the code does not work and indicates how to do it correctly. - razielsd
  • If it is not true - therefore you know the reason, since you know the reason - then why is there no right answer here? Here comes the author of the question and will be able to clarify the situation, or are you the author and sit under two accounts? - razielsd
  • Please note, my answer may help the author, your comments are not at all of any practical use, despite your knowledge of several reasons, only offtopic lit. - razielsd