My login.php page not redirecting to my index.php file

When i login with my credentials it doesn’t redirect me to my index.php page as it should according to my code. Instead it remains on the login.php page.
here is my server.php code snippet(login part):

// LOGIN USER
if (isset($_POST['login_user'])) {
  $username = mysqli_real_escape_string($db, $_POST['username']);
  $password = mysqli_real_escape_string($db, $_POST['password']);

  if (empty($username)) {
  	array_push($errors, "Username is required");
  }
  if (empty($password)) {
  	array_push($errors, "Password is required");
  }

  if (count($errors) == 0) {
  	$password = md5($password);
  	$query = "SELECT * FROM access WHERE username='$username' AND password='$password'";
  	$results = mysqli_query($db, $query);
  	if (mysqli_num_rows($results) == 1) {
  	  $_SESSION['username'] = $username;
  	  $_SESSION['success'] = "You are now logged in";
  header('Location: index.php');
  	}else {
  		array_push($errors, "Wrong username/password combination");
  	}
  }
}






and here is my index.php code snippet(first part):
<?php 
  session_start(); 

  if (!isset($_SESSION['username'])) {
  	$_SESSION['msg'] = "You must log in first";
  	header('location: login.php');
  }
  if (isset($_GET['logout'])) {
  	session_destroy();
  	unset($_SESSION['username']);
  	header("location: login.php");
  }
?>
<!DOCTYPE html>
<html>


Hi @abir!

Use header("Location: index.php", true, 301); instead.

1 Like

thank you so much @teodor. But the interesting part is, the website works just works fine when it is accessed from elsewhere like from my friend’s computer (with the previous code). But only showing issues with my pc.

Clear your browser cache and try again. In addition you can send a header to instruct your browser to not keep the redirect in cache:

header("Cache-Control: no-cache, no-store", true);