简体   繁体   中英

Unable to redirect my LOGIN page to home page

I am facing the problem for my login page. Even after entering correct username and password it is not redirecting to my home page (index.php).

index.php:

<?php 
  if (!isset($_SESSION['login_username']))
   {
    $_SESSION['msg'] = "You must log in first";
    header('location: login.php');
  }

  if (isset($_GET['logout'])) 
  {
    session_destroy();
    unset($_SESSION['login_username']);
    header("location:login.php");
  }

?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="content">
    <?php if(isset($_SESSION['success'])): ?>
      <div class="error success"> 
        <h3>
          <?php 
            echo $_SESSION['success'];
            unset ($_SESSION['success']);
          ?>
        </h3>
      </div>
    <?php endif ?>

    <?php if (isset($_SESSION["login_username"])): ?>
  <p>Welcome <strong><?php echo $_SESSION['login_username']?></strong></p>
  <p><a href="index.php?logout='1'" style="color: red;">Logout</a></p>
<?php endif ?>
</body>
</html>

login.php:

<?php include('connect.php')?>

<!DOCTYPE html>
<html>
    <head>

    </head>

    <body>
        <div class="header">
            <h2>ADMIN LOGIN</h2>
        </div>

        <form method="post" action="login.php" name ="login">

            <div class="input-group">
                <label>UserName</label>
                    <input type="varchar" required="required" name="login_username">
            </div>

            <div class="input-group">
                <label>Password</label>
                    <input type="password" required="required" name="login_password" >
            </div>

            <div class="input-group">
                <button type="submit" class="btn" name="submit_login">login</button>
            </div>

        </form>
    </body>
</html>

connect.php:

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<?php


$username= "root";
$password = "";

$errors = array();

$db=mysqli_connect('localhost','root','','db1');


if (isset($_POST['submit_login'])) 
    {
      $login_username = mysqli_real_escape_string($db, $_POST['login_username']);
      $login_password = mysqli_real_escape_string($db, $_POST['login_password']);
  $sql = "SELECT * FROM login_details WHERE login_username='$login_username' AND login_password='$login_password'";

        $result = $db->query($sql);


        if ($result->num_rows == 1) 
        {
          $_SESSION['login_username'] = $login_username;
          $_SESSION['success'] = "You are now logged in";

          Header( 'Location: index.php' );
        }
        else 
        {
            echo "Wrong username/password combination";
        }
}
</body>
</html>

Everything else is working fine , like when Wrong username/password combination is used the msg is displayed but when correct combination is used nothing happens.

You are checking for $_SESSION["username"] in index.php, however, in connect.php you are setting $_SESSION['login_username']

Change $_SESSION['login_username'] to $_SESSION['username']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM