Lara_Love's avatar

php mysqli error

hello all i have login form but not Redirect to user dashboard page

can you read it and help me to solve error ?

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Login</title>
    <link rel="stylesheet" href="style.css"/>
</head>
<body>
<?php
    require('db.php');
    session_start();
    if (isset($_POST['login'])) {
        $username = stripslashes($_REQUEST['username']);    // removes backslashes
        $username = mysqli_real_escape_string($con, $username);
        $password = stripslashes($_REQUEST['password']);
        $password = mysqli_real_escape_string($con, $password);
        // Check user is exist in the database
        $query    = "SELECT * FROM `login` WHERE username='$username'
                     AND password='" . md5($password) . "'";
        $result = mysqli_query($con, $query) or die(mysql_error());
        $rows = mysqli_num_rows($result);
        if ($rows == 1) {
            $_SESSION['username'] = $username;
            // Redirect to user dashboard page
            header("Location: dashboard.php");
        } else {
            echo "<div class='form'>
                  <h3>Incorrect Username/password.</h3><br/>
                  <p class='link'>Click here to <a href='login.php'>Login</a> again.</p>
                  <p class='link'>Click here to <a href='logout.php'>Log OUT</a> again.</p>
                  </div>";
        }
    } else {
?>
    <form class="form" method="post" name="login">
        <h1 class="login-title">Login</h1>
        <input type="text" class="login-input" name="username" placeholder="Username" autofocus="true"/>
        <input type="password" class="login-input" name="password" placeholder="Password"/>
        <input type="submit" value="Login" name="submit" class="login-button"/>
        <p class="link">Don't have an account? <a href="registration.php">Registration Now</a></p>
  </form>
<?php
    }
?>
</body>
</html>
0 likes
8 replies
vincent15000's avatar

Why are you working with such a code ?

That's not a code that follows the Laravel logic.

Lara_Love's avatar

@vincent15000 There is a student project / unfortunately I have to solve it / what is the problem, it does not work and does not go to the dashboard

1 like
vincent15000's avatar
Level 63

@LoverToHelp You should move this post to the code review category, it's not a Laravel problem.

First check this.

if ($rows == 1) {
	var_dump('ok');
    $_SESSION['username'] = $username;
    // Redirect to user dashboard page
    header("Location: dashboard.php");
} else {
	var_dump('ko');
    echo "<div class='form'>
          <h3>Incorrect Username/password.</h3><br/>
          <p class='link'>Click here to <a href='login.php'>Login</a> again.</p>
          <p class='link'>Click here to <a href='logout.php'>Log OUT</a> again.</p>
     </div>";
}

I have added var_dump at two places.

Lara_Love's avatar

@vincent15000 I replaced this code and the whole page disappeared / I wish it was Laravel / I saw a code like this about six or seven years ago

1 like
vincent15000's avatar

@LoverToHelp That's not a Laravel code. If you want to do the same with Laravel, you need to code like the Laravel logic. Then put your business logic inside a controller to login or better use a package like Fortify to login.

If it's just for learning purposes, you can then check the path to access to the dashboard.php file.

Lara_Love's avatar

@vincent15000 dashboard.php

<?php
include("auth_session.php");
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Dashboard - Client area</title>
    <link rel="stylesheet" href="style.css" />
</head>
<body>
    <div class="form">
        <p>Hey, <?php echo $_SESSION['username']; ?>!</p>
        <p>You are in user dashboard page.</p>
        <p><a href="logout.php">Logout</a></p>
    </div>
</body>
</html>

auth_session.php

<?php
    session_start();
    if(!isset($_SESSION["username"])) {
        header("Location: login.php");
        exit();
    }
?>

1 like
vincent15000's avatar

@LoverToHelp What's the path to access to the dashboard file ? I think that the code of the dashboard doesn't matter for the moment. What doesn't work is in your login page code.

Please or to participate in this conversation.