0

My php code is not processing the login verification and sticks to the login_action.php page where the form action is performed. It is suppose to redirect to next page if login credentials match and alert messages otherwise. This is my login_action form php.

<?php
session_start();
include("connect.php");

session_unset($_SESSION['ok']);
session_unset($_SESSION['id']);
session_unset($_SESSION['user_id']);

$un = mysqli_real_escape_string($conn, $_POST['un']);
$pw = mysqli_real_escape_string($conn, $_POST['pw']);

$query = mysqli_query("SELECT * FROM tbl_users WHERE (username='$un' or email='$un')") or die(mysqli_error());
$num = mysql_num_rows($query);
if($num == 1)
{
    while($row = mysqli_fetch_assoc($query)){
        if(password_verify($pw, $row['password'])){
            $_SESSION['submit']='ok';
            $_SESSION['id']=$query['user_id'];
            header("location:dashboard.php");
            exit();
        }
    }
}
else
{
    header("location:index.php?msg=wrong");
}

    ?>

and this is my screen after entering matching login credentials. enter image description here

UPDATE: The code started executing now I don't know how because I haven't made any changes but the next time I tried logging in again mysqli_connect gave me No database selected error. This is my connect.php

<?PHP
    error_reporting(0);
    $conn = mysqli_connect("localhost","root","", "sts_nlg_test") or die('Cannot find database.');
?>
beginner92
  • 11
  • 1
  • 5
  • PHP is a case sensitive language. If you look at an example of someone trying to do something similar, like the one linked at the end of my comment, you will see that they have capitalised the 'L' in location. Try your code but with a capital L in location for your header lines. https://www.tutorialrepublic.com/faq/how-to-make-a-redirect-in-php.php – Kneecaps Dec 27 '21 at 08:38
  • @CharedAssassin I already came across this post and it doesn't work...i tried doing it with capital L too – beginner92 Dec 27 '21 at 08:43
  • do you turn off the error message? try `echo 'a'; die;` every success login proses so you can know what is the actual problem – Atoli Gaming Dec 27 '21 at 08:44
  • or your header code should be `header("Location: index.php?msg=wrong");` – Atoli Gaming Dec 27 '21 at 08:46
  • 3
    It's a typo: `mysql_num_rows(...)`. [Why shouldn't I use mysql_* functions in PHP?](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php?noredirect=1&lq=1) They don't work together with `mysqli_*` functions and have effectively been removed since PHP7 – brombeer Dec 27 '21 at 08:48
  • @beginner92 maybe try adding in `echo 'this worked';` lines at certain points in your code. One thing that often got me when I first started with PHP was I assumed a problem was at a certain line (like your `header(Location ...` line though actually the code had an earlier error and wasn't getting to that line. Try finding out where the code gets to before it stops because most PHP versions won't execute anything after an error – Kneecaps Dec 27 '21 at 08:49
  • For better debugging please see [How do I get PHP errors to display?](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display?rq=1) – brombeer Dec 27 '21 at 08:50
  • @CharedAssassin "_PHP is a case sensitive language_" Not quite, `Echo "foo";` will do the same as `echo "foo";` – brombeer Dec 27 '21 at 09:08

0 Answers0