Опишу ситуацию в 4 этапа. Функция "emptyInputLogin" работает корректно. Подключение к базе тоже работает.
login.php
<?php include_once 'header.php'; ?> <section class="signup-form"> <h1>Log in</h1> <form action="includes/login.inc.php" method="POST"> <input type="text" name="uid" placeholder="Username/email"> <input type="password" name="pwd" placeholder="Password"> <button type="submit" name="submit">Log In</button> </form> <?php if (isset($_GET["error"])) { if ($_GET["error"] == "emptyinput") { echo "<p>Fill all</p>"; } elseif ($_GET["error"] == "wronglogin") { echo "<p>In correct log in</p>"; } } ?> </section> <?php include_once 'footer.php'; ?>
login.inc.php
<?php if (isset($_POST["submit"])) { $username = $_POST["uid"]; $pwd = $_POST["pwd"]; include_once 'db.inc.php'; include_once 'function.inc.php'; if (emptyInputLogin($username, $pwd) !== false) { header("location: ../login.php?error=emptyinput"); exit(); } loginUser($conn, $username, $pwd); } else { header('location: ../login.php'); exit(); }
function.inc.php
- функцияloginUser()
function loginUser($conn, $username, $pwd) { $uidExists = uidExists($conn, $username, $username); if ($uidExists === false) { header("location: ../login.php?error=wronglogin"); exit(); } $pwdHashed = $uidExists["usersPwd"]; $checkPwd = password_verify($pwd, $pwdHashed); if ($checkPwd === false) { header("location: ../login.php?error=wronglogin") exit(); } elseif ($checkPwd === true) { session_start(); $_SESSION['userid'] = $uidExists['userId']; $_SESSION['useruid'] = $uidExists['usersUid']; header("location: ../index.php"); exit(); } }
function.inc.php
- функцияuidExists()
function uidExists($conn, $username, $email) { $sql = "SELECT * FROM users WHERE usersUid = ? OR usersEmail = ?;"; $stmt = mysqli_stmt_init($conn); if (!mysqli_stmt_prepare($stmt, $sql)) { header("location: ../signup.php?error=stmtfailed"); exit(); } mysqli_stmt_bind_param($stmt, "ss", $username, $email); // запуск запроса mysqli_stmt_execute($stmt); $resultData = mysqli_stmt_get_result($stmt); if ($row = mysqli_fetch_assoc($resultData)) { return $row; } else { $result = false; return $result; } mysqli_stmt_close($stmt); }