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
You can try to substitute
by
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:
then in your landing page you can check if there is a user and that user is logged in.
otherwise show landing page
From the php header() page in the php manual:
https://www.php.net/manual/en/function.header.php