-3

I am beginner in PHP and I`m getting some error in index.php

"This page isn’t working localhost redirected you too many times. Try clearing your cookies. ERR_TOO_MANY_REDIRECTS"

I have searched all over internet and trying all things, but didn't get any result. Please give me some solution for this problem.

<?php
session_start();
include('admin/connect.php');
include('includes/header.php');
$_SESSION['username'];
$profile = $_SESSION['username'];
if($profile==true){ 
} else{
    header('location: index.php');
}   
$query = "SELECT * FROM post";  
$return = mysqli_query($conn, $query);
if(!$return){
    die('Data Could not be fetched'.mysqli_error());
} 
while($row = mysqli_fetch_array($return)){
    $id = $row['id'];
    $p_title = $row['p_title'];
    $author = $row['author'];
    $category = $row['category'];
    $content = $row['content'];
    $date = $row['date'];   
   ?>
<div class="blog-div">
<div id="latest-div">
   <h1 id="post-title"><?php echo 
   $p_title; ?></h1>
   <p id="post-info">By: <span><?php echo 
   $author; ?></span> | In: <span><?php echo $category; ?></span> | Last 
   updated: <?php echo $date; ?></p>
                <p><?php echo $content; ?></p>
                <a id="read-more-btn" href="blog.php? 
   id=<?php echo $id; ?>">Read More</a>
            </div>
            <?php } ?>  
        </div>
        <div id="sidebar-div">
        <h3>Latest Post</h3>
   <?php
    $query = "SELECT * FROM post";
$return = mysqli_query($conn, $query);
    if(strlen(!$return)){
    die('Data Could not be fetched'.mysqli_error());
} 
    while($row = mysqli_fetch_array($return)){
    $p_title = $row['p_title'];
    $author = $row['author'];
    $category = $row['category'];
    $content = $row['content'];
    $date = $row['date'];
     ?>
<p><?php echo $p_title; ?></p>
     <?php } ?>
    </div>  
   <?php
include('includes/footer.php');
   ?>
Mouse on the Keys
  • 322
  • 1
  • 5
  • 13
  • 1
    Please follow the steps provided by [this question and answer](https://stackoverflow.com/q/1053424/5914775) to enable error reporting. This will tell you that `$_SESSION['username'];` has an undefined index causing the subsequent if-else statement to follow the _false_ part resulting in an infinite redirect loop. – Tom Udding Aug 02 '19 at 07:35

2 Answers2

-1

Can you tell me what is the file name? If current code file name is index.php it will do forever redirect. You need to check this line

if($profile==true){ 
} else{
    header('location: index.php');
}

The biggest possibility cause the forever redirect.

William Gunawan
  • 748
  • 3
  • 8
  • This is true, however, it is more important to know why it would end up in the second part of this if-else statement. That is because `$profile` is `NULL` due to `username` being an undefined index in `$_SESSION`. As mentioned [here](https://stackoverflow.com/questions/57321777/how-to-fix-err-too-many-redirects-in-localhost-in-php#comment101134433_57321777). – Tom Udding Aug 02 '19 at 07:42
  • Thanks sir, My Problem is solved by removing if($profile==true){ } else{ header('location: index.php'); } – Mahendra Sangawa Aug 02 '19 at 08:07
-2

I facced same issue then i added $conn->close(); at the end of php code, which closes the connection.

Raman
  • 624
  • 5
  • 13