0

I am experiencing some issues with php, on my localhost. I receive a err_too_many_redirects error from my browser.

I am recieving this error when I am trying to direct a user from accessing information from a different place on the web server.

Here is the code:

<?php
session_start();
if(isset($_SESSION['valid'])) {
    $loggedIn = $_SESSION['name'];
    if($loggedIn != basename(getcwd())) {
        echo "You are in the wrong place.";
        $url = '../../users/' . $loggedIn . '/index.php';
        header('Location: ' . $url);
    } else {
        echo "Hello";
    }
} else {
    $url = 'index.php';
    header('Location: ' . $url);
}
?>

Can someone please explain to me why it is doing this? I have tried multiple things such as re-arranging the order of the process.

Can someone please help me?

  • That means that you have a redirection loop, The script is redirecting to itself over and over. at some point, the script just stop because it reachs it's max redirection point, is your code directly in the index.php ? – Nicolas Mar 29 '17 at 14:10
  • Where is this code located? In your index? In either one of the locations you are trying to redirect to, there's a redirect again. Check for that. – Qirel Mar 29 '17 at 14:10
  • In your browser's debugging tools, what are the redirects being performed? Usually this is a result of an "infinite loop" of redirects, where one page redirects to another, which itself redirects back to the first one. This code alone doesn't demonstrate that. – David Mar 29 '17 at 14:10
  • This cannot be the full code. – Rotimi Mar 29 '17 at 14:12
  • None of this is helping, I am attempting to redirect them to direct them straight to the index (home page) which has php just for starting a session and editing the navigation bar with a username IF they are signed in, how would this result in a "infinite loop" when the index (home page) would not specifically direct them somewhere else? – Myles Melia Mar 29 '17 at 14:14
  • you're outputting before header inside `if($loggedIn != basename(getcwd())) {...}`. If you don't know what that means, Google "headers already sent..." – Funk Forty Niner Mar 29 '17 at 14:14
  • and post your `.htaccess` file for this, because that's also a related item/issue, this besides what I already said above but you either didn't want to respond or have left the question. – Funk Forty Niner Mar 29 '17 at 14:22

1 Answers1

-1

My bad, silly mistake.

I was re-directing them straight back to the INDEX page of the user

Code:

<?php
session_start();
if(isset($_SESSION['valid'])) {
    $loggedIn = $_SESSION['name'];
    if($loggedIn != basename(getcwd())) {
        echo "You are in the wrong place.";
        $url = '../../users/' . $loggedIn . '/index.php';
        header('Location: ' . $url);
    } else {
        echo "Hello";
    }
} else {
    $url = 'index.php';
    header('Location: ' . $url);
}
?>

I had to update the URL here in the else statement for the if(isset($_SESSION['valid]')) Here it is:

$url = '../../index.php';
header('Location: ' . $url);
  • Ok hold on, you mean to say that you removed `echo "You are in the wrong place.";` or you removed that and had to change your `$url = '../../users/' . $loggedIn . '/index.php';` to `$url = '../../index.php';`? Your answer is unclear and short on detail. This for future readers. – Funk Forty Niner Mar 29 '17 at 14:26
  • If you kept `echo "You are in the wrong place."; $url = '../../users/' . $loggedIn . '/index.php'; header('Location: ' . $url);` that will **not** work, did you know that? So, is that part still present in your code now? – Funk Forty Niner Mar 29 '17 at 14:30
  • This echo "You are in the wrong place."; is the one you should be removing – kourouma_coder Mar 29 '17 at 14:34
  • Listen, it works now, the way I want it to, and I changed the else statement on the `if(isset($_SESSION['valid'])) { ... } else { }` – Myles Melia Mar 29 '17 at 14:40
  • And the code works with echo "You are in the wrong place." It still directs the user to THEIR page when they are trying to view another users. – Myles Melia Mar 29 '17 at 14:42