自昨晚以来,我仅在我的index.php页面上一直收到此错误。我已经清除了所有cookie,但是仍然无法在本地主机上访问index.php。我已经看过其他解决方案,但找不到答案。
我认为这与我的index.php代码有关,该代码将我重定向到同一页面进行登录。但是我不熟悉Web开发,所以我不确定如何解决该问题。下面,我提供了我的index.php和login.php的代码段。非常感谢您的帮助。
login.php
<?php
session_start();
// Change this to your connection info.
$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASS = '';
$DB_NAME = 'ecom';
// Try and connect using the info above.
$con = mysqli_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if ( mysqli_connect_errno() ) {
// If there is an error with the connection, stop the script and display the error.
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
// Now we check if the data from the login form was submitted, isset() will check if the data exists.
if ( !isset($_POST['username'], $_POST['password']) ) {
// Could not get the data that should have been sent.
die ('Please fill both the username and password field!');
}
if ($stmt = $con->prepare('SELECT id, password FROM users WHERE username = ?')) {
// Bind parameters (s = string, i = int, b = blob, etc), in our case the username is a string so we use "s"
$stmt->bind_param('s', $_POST['username']);
$stmt->execute();
// Store the result so we can check if the account exists in the database.
$stmt->store_result();
}
if ($stmt->num_rows > 0) {
$stmt->bind_result($id, $password);
$stmt->fetch();
// Account exists, now we verify the password.
// password_hash function in is used in registration file to store the hashed passwords.
if (password_verify($_POST['password'], $password)) {
// Verification success! User has loggedin!
// Create sessions so we know the user is logged in, they basically act like cookies but remember the data on the server.
session_regenerate_id();
$_SESSION['loggedin'] = TRUE;
$_SESSION['name'] = $_POST['username'];
$_SESSION['id'] = $id;
/*echo 'Welcome ' . $_SESSION['name'] . '!';*/
header('Location: index.php');
} else {
echo 'Incorrect password!';
}
} else {
echo 'Incorrect username!';
}
$stmt->close();
?>
index.php
<?php
// We need to use sessions, so you should always start sessions using the below code.
session_start();
// If the user is not logged in redirect to the login page...
if (!isset($_SESSION['loggedin'])) {
header('Location:index.php');
exit();
}
?>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<title>City Outlet</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<link href="css/style.css?10" rel="stylesheet" type="text/css">
<!-- awesome-font CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<!--========== Top bar Area ============-->
<header class="topbar">
<nav>
<a style="color:white; font-size:18" href="logout.php"><i class="fa fa-sign-out style="font-size:24px; color:white;""></i>Logout</a>
<button href="" class="btn login btn-default d-inline-flex" data-toggle="modal" data-target="#loginModal" ><i class="fa fa-sign-in" style="font-size:24px" value="login" onClick="showModal()" ></i> Login</button>
<button href="" class="btn login btn-default d-inline-flex" data-toggle="modal" data-target="#signupModal"><i class="fa fa-user" style="font-size:24px" ></i> Sign Up</button>
</nav>
<p style="color:white; font-weight:bold; text-align:right; line-height:-10px; margin-top: 7px;"> Welcome: <strong style="color:red; font-weight:bold;"><?=$_SESSION['name']?></strong>! <img src="images/ava.png" style=width:25px; height="25px;"> </p>
</header>
<!-- Login Modal -->
<div class="loginbox" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
aria-hidden="true"> <img class="avatar" src="images/redavatar.png">
<h1>Sign in</h1>
<form action="login.php" method="post">
<p>Username</p>
<input type="text" name="username" placeholder="Enter username" required>
<p>Password</p>
<input type="password" name="password" placeholder="Enter password" required>
<input type="submit" name="submit" value="submit">
<a href="signup.php">Not a member?</a> <br>
<br>
</form>
</div>
<!----- Sign up Modal ----->
<div class="container">
<h1 class="text-white bg-dark text-center"></h1>
<div class="modal" id="signupModal">
<div class="modal-dialog ">
<div class= "modal-content">
<div class="modal-header">
<h3 >Sign Up</h3>
<button style="color:red" type="button" class="close" data-dismiss="modal" > ×</button>
</div>
<div class="modal-body">
<form action="signup.php" method="post" >
<div class="form-group">
<label><i class="fa fa-user fa-2x"></i> First name:
</label>
<input type="text" name="username" class="form-control" placeholder="your name" required>
</div>
<div class="form-group">
<label><i class="fa fa-lock fa-2x"></i> Password:
</label>
<input type="password" name="password" class="form-control" placeholder="choose password" required>
</div>
<div class="form-group">
<label><i class="fa fa-envelope fa-2x"></i> Email:
</label>
<input type="text" name="email" class="form-control" placeholder="Your email" required>
</div>
<input type="submit">
</form>
</div>
</div>
</div>
</div>
</div>
<!----- Sign up Modal ----->
<!--========== End Top bar Area ============-->
<nav class="navbar fixed-top navbar-expand-md navbar-light ">
<div class="container-fluid"> <a class="navbar-brand " href="index.php"> <img src="images/citylogo.png" alt=""> </a>
<button class="navbar-toggler third-button darken-3" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<div class="animated-icon3"><span></span><span></span><span></span></div>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item active"> <a class="nav-link" href="index.php">Home <span class="sr-only">(current)</span></a> </li>
<li class="nav-item"> <a class="nav-link" href="#">Services</a> </li>
<li class="nav-item"> <a class="nav-link" href="shop.php">Shop</a> </li>
<li class="nav-item"> <a class="nav-link" href="booking.php">Booking</a> </li>
<li class="nav-item"> <a class="nav-link" href="gallery.html">Gallery</a> </li>
<li class="nav-item"> <a class="nav-link" href="contact.html">Contact</a> </li>
</ul>
<form class="searchbar" action="/action_page.php" style="margin:auto;max-width:300px">
<button type="submit"><i class="fa fa-search"></i></button>
<input type="text" placeholder="Search.." name="search2">
</form>
</div>
</div>
</nav>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
<script>
function showModal()
{
document.getElementById("loginModal").style.display = 'block';
}
</script>
<script>
// Get the modal
var modal = document.getElementById('id01');
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</body>
</html>
答案 0 :(得分:0)
在index.php上 这部分代码。
if (!isset($_SESSION['loggedin'])) {
header('Location:index.php'); // redirecting to same page.
exit();
}
如果用户未登录,您将重定向到同一页面,这就是为什么您进行无限重定向...您必须重定向到其他页面(如登录页面)。