skip to Main Content

redirect to the home page.

hi, I have 2 php files, I’m working on my project login system.

when I try to input my email and password, it still goes to my login page even though the information is correct

here are my two php codes

(login.php)

<?php 
    require 'database.php';
    if(!empty($_SESSION['id'])){
        header("Location: index.php");
        exit();
}
 ?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=chrome">
        <meta name="viewport" content="width=device-width", initial-scale="1.0">
        <title>Car&Go</title>
        <link rel="stylesheet" href="style.css">
        <link rel="stylesheet" type="text/css" href="fontawesome-free-6.4.0-web/css/all.css">


        
    </head>     
    <body>
        <header>
            <a href="index.php#home" class="logo"><img src="img/jeep.png" alt=" "></a>
            <div class="bx bx-menu" id="menu-icon"></div>
            <ul class="navbar">
               <li><a href="index.php">Exit</a></li>
            </ul>
            

        </header>

        <div class="login-form-container">
        <?php
            
            if(isset($_POST["login"])){
            $email = $_POST["email"];
            $password = $_POST["password"];

            $result = mysqli_query($conn, "SELECT * FROM cargo WHERE email = '$email'");
            $row = mysqli_fetch_assoc($result);
            if(mysqli_num_rows($result) > 0){
            if($password == $row['password']){
            $_SESSION["login"] = true;
            $_SESSION["id"] = $row["id"];
            header("Location: login_index.php");
    }
    
            else{
            echo"<div class='alert alert-success'>Wrong Password</div>";
    }
  }

            else if(empty($email) OR empty($password)){
            echo"<div class='alert alert-success'>All fields are required</div>";
  }
            else{
                echo"<div class='alert alert-success>User Not Registered</div>";
            }
}
?>

    
    <form action="login.php" method="post">
    <h3>Login</h3>
        <input type="email" name="email" placeholder="Email" class="box">
        <input type="password" name="password" placeholder="Password" class="box">
        <span class="checkbox">
            <input type="checkbox" id="check"/>
            <label for="check">Remember me</label>
        </span>
        <p><a href="forgot.php">Forgot password?</a></p>
        <input type="submit" name="login" value="Log In" class="btn">
        <div class="login_signup">
            Don't have an account?<a href="signup.php" id="signup">  Sign Up</a>
        </div>
    </form>
</div>


        
    </body>
</html>

this next one is my landing page, where login.php should go after putting the correct login info.

(login.index).php

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

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=chrome">
        <meta name="viewport" content="width=device-width", initial-scale="1.0">
        <title>Car&Go</title>
        <link rel="stylesheet" href="style.css">
        <link rel="stylesheet" type="text/css" href="fontawesome-free-6.4.0-web/css/all.css">

        
    </head>     
    <body>
        <header>
            <a href="login_index.php" class="logo"><img src="img/jeep.png" alt=" "></a>
            <div class="bx bx-menu" id="menu-icon"></div>
            <ul class="navbar">
               <li><a href="login_index.php#home">Home</a></li>
               <li><a href="login_index.php#ride">Ride</a> </li>
               <li><a href="login_services.php">Services</a></li>
               <li><a href="Listing.php">Add Car</a></li>
               <li><a href="login_index.php#about">About</a></li>
            </ul>
            
            <div class="header-btn">
                <a href="logout.php" class="sign-up">Logout</a> 
            </div>

        </header>

<!--Home Paege-->

        <section class="home" id="home">
            <div class="text">
                <h1><span>Looking</span> to <br> Rent a <span>car</span>?</h1>
            </div>
            
            <div class="form-container">
            <form autocomplete="off" action="services.php">
                <div class="input-box">
                    <span>Location</span>
                    <input type="search" name="cities" id="myInput" placeholder="Search Places">
                </div>
                <div class="input-box">
                    <span>Pick-Up Date</span>
                    <input type="date" name="" id="">
                </div>
                <div class="input-box">
                    <span>Return Date</span>
                    <input type="date" name="" id="">
                </div>
                <input type="submit" name="" id="" class="btn">                 
            </form>
             </div>
             <script src="autocomplete.js"> </script>
        </section>

<!--Our Ride-->

        <section class="ride" id="ride">
                </div>
            <div class="heading">
                <span>How It Works?</span><h1>Rent With 3 Easy Steps</h1>
            </div>
        <div class="ride-container">
            <link href='https://unpkg.com/[email protected]/css/boxicons.min.css' rel='stylesheet'>
            <div class="box">
                <i class='bx bxs-map bx-tada' ></i>
                <h2>Choose A Location</h2>
                <p>To choose a rental location, please enter your desired city or airport in the search bar and select from the available options that appear. Note that the location search is limited to 200 characters.</p>
                </p>
            </div>

            <div class="box">
                <i class='bx bxs-calendar-check bx-tada' ></i>
                <h2>Pick-Up Date</h2>
                <p>Select the pick-up date for your rental car. Our system will show you available cars for the dates you have selected. Please note that the pick-up date must be at least 24 hours in advance and cannot exceed 30 days from today's date. </p>
            </div>

            <div class="box">
                <i class='bx bxs-calendar-star bx-tada' ></i>                
                <h2>Book A Car</h2>
                <p>Once you have selected your preferred pickup location and date, you can now browse through our range of available rental cars and choose the one that best suits your needs. Select the vehicle that you would like to rent, and provide any additional details such as insurance, fuel options, and any special requests..</p>
            </div>
        </div>
        </section>

<!--MEET THE TEAM-->

<section class="about" id="about">
    <div class="heading">
      <span> ABOUT US</span>
      <h1>Meet The Team</h1>
    </div>
    <div class="about-container">

    <div class="box">
        <div class="about-img">
            <img src="img/kobe.jpg" alt="">
        </div>
    </div>

    <div class="box">
        <div class="about-img">
            <img src="img/rex.jpg" alt="">
        </div>
    </div>

    <div class="box">
    
        <div class="about-img">
            <img src="img/marcus.jpg" alt="">
        </div>
    </div>

    <div class="box">
        <div class="about-img">
            <img src="img/jc.jpg" alt="">
        </div>
    </div>
</div>
           <div class="about-text">
            <span> Meet The 5</span>
            <p> </p>
            <a href="#" class="btn">Learn More</a>
           </div>
    
</section>

<!--Reviews-->

   <section class="reviews" id="reviews">
    <div class="heading">
        <span>Reviews</span>
        <h1>What our Customer Say!</h1>
    </div>
   </section>
   
<!--Newsletter-->

<section class="newsletter">
    <h2>Subscribe To Newsletter</h2>
    <div class="box">
        <input class="text" placeholder="Enter your email.">
        <a href="#" class="btn">Subscribe</a>
    </div>
</section>

<!--Copyright-->

<div class="copyright">
    <p>@Car&Go All Right Reserved</p>
    <div class="social">
        <a href="#"><i class='bx bxl-facebook'></i></a>
        <a href="#"><i class='bx bxl-twitter' ></i></a>
        <a href="#"><i class='bx bxl-instagram-alt' ></i></a>
    </div>

</div>
   
<!--LINK TO JS-->

        <script src="main.js"></script> 

    </body>
</html> 

3

Answers


  1. You can try to substitute

    header("Location: login_index.php");
    

    by

    echo "<script> window.location='login_index.php';</script>";
    
    Login or Signup to reply.
  2. First, you need to start session in your login page, there’s null there.
    secondly in your landing page you are checking for $_SESSION[‘user’] that you didn’t set in your logging script it needs to be set. For example:

    login script:

    if($password == $row['password']){
        $_SESSION["login"] = true;
        $_SESSION["id"] = $row["id"];
        $_SESSION['user'] = $row["anyValueHere"];
    }
    

    then in your landing page you can check if there is a user and that user is logged in.

    if (!isset($_SESSION['user']) && $_SESSION["login"] === false) {
        header("Location: login.php");
        exit();
     }
    

    otherwise show landing page

    Login or Signup to reply.
  3. From the php header() page in the php manual:
    https://www.php.net/manual/en/function.header.php

    Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file. >

    Try putting all your php code at the top of the page before your html output.
    Like this:

    <?php
    session_start();
    require 'database.php';
    if(!empty($_SESSION['id'])){
        header("Location: index.php");
        exit();
    }
    
    $html_output = ''; // set the variable to empty for security purposes.
    if(isset($_POST["login"])){
        $email = $_POST["email"];
        $password = $_POST["password"];
    
        $result = mysqli_query($conn, "SELECT * FROM cargo WHERE email = '$email'");
        $row = mysqli_fetch_assoc($result);
        if(mysqli_num_rows($result) > 0){
            if($password == $row['password']){
                $_SESSION["login"] = true;
                $_SESSION["id"] = $row["id"];
                header("Location: login_index.php");
                exit();
            } else{
                $html_output = "<div class='alert alert-success'>Wrong Password</div>";
            }
        } else if(empty($email) OR empty($password)){
            $html_output =  "<div class='alert alert-success'>All fields are required</div>";
        } else {
            $html_output = "<div class='alert alert-success>User Not Registered</div>";
        }
    }
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta http-equiv="X-UA-Compatible" content="IE=chrome">
            <meta name="viewport" content="width=device-width", initial-scale="1.0">
            <title>Car&Go</title>
            <link rel="stylesheet" href="style.css">
            <link rel="stylesheet" type="text/css" href="fontawesome-free-6.4.0-web/css/all.css">
        </head>
        <body>
        <header>
            <a href="index.php#home" class="logo"><img src="img/jeep.png" alt=" "></a>
            <div class="bx bx-menu" id="menu-icon"></div>
            <ul class="navbar">
                <li><a href="index.php">Exit</a></li>
            </ul>
        </header>
        <div class="login-form-container">
            <?php echo $html_output;  // output the error messages from above.?>
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search