php " this webpage has a redirect loop "

I am using the following code in  login.php
if (isset($_SESSION['username'])){
  if($_SESSION['type'] != "Admin"){
header('Location: panel/index.php');
}
else if($_SESSION['type'] != "Manager"){
header('Location: admin/index.php');
}
else 
{
header('Location: Manager/index.php');
}

Open in new window

I get the error during login
this webpage has a redirect loop ( chrome )
webpage isn't  redirecting properly  (Firefox )
prashanth agsoftware engineer Asked:
Who is Participating?
 
Ray PaseurCommented:
From where I sit it looks like the logic may be confused.  You might want to learn about coding standards, especially lining up the control structures (those curly brackets) so that you can get a visual indication of the logic paths in the programming.  You might want to use comments to write out what you expect the programming to do, before you start writing the programming.  These two techniques are always used by professionals because these techniques help us get it right the first time.

I notice that you're sending header() location statements to redirect the browser.  If you look at the PHP man page for that function, you will see examples that use exit or die() after the redirection headers.  The reason for this is that header() is a synchronous part of the PHP script.  It sends the header and the script keeps right on running, perhaps for an unpredictable period of time, until the browser receives the command and redirects to the new URL.  So when you want immediate redirection, you need to follow the examples shown in the PHP manual.

This is what the programming is doing, annotated with comments
/**
 * TEST FOR username
 */
if (isset($_SESSION['username']))
{
    /**
     * THIS CODE PATH WILL BE ENTERED IF username IS DEFINED
     * TEST FOR type != Admin
     */
    if($_SESSION['type'] != "Admin")
    {
        /**
         * THIS REDIRECT WILL BE SENT IF type != Admin
         */
        header('Location: panel/index.php');
    }
    /**
     * THIS CODE PATH WILL BE ENTERED IF type == Admin
     * TEST FOR type != Manager
     */
    else if($_SESSION['type'] != "Manager")
    {
        /**
         * THIS REDIRECT WILL BE SENT IF type == Admin AND type != Manager
         */
        header('Location: admin/index.php');
    }
/**
 * THIS CODE PATH WILL BE ENTERED IF username IS UNDEFINED
 * OR IT MAY BE ENTERED (UNPREDICTABLE) IF THE PREVIOUS REDIRECTS HAVE NOT COMPLETED
 */
else 
{
    /**
     * THIS REDIRECT WILL BE SENT IF username IS UNDEFINED
     */
    header('Location: Manager/index.php');
}
/**
 * THE SCRIPT MAY GET HERE (UNPREDICTABLE) IF THE PREVIOUS REDIRECTS HAVE NOT COMPLETED
 */

Open in new window


Since it looks like you're trying to work out some type of PHP client authentication, you might want to read this article.  It shows the general design pattern and has tested code examples.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391-PHP-login-logout-and-easy-access-control.html

If you're new to PHP and want some good learning resources, this article can help.  Perhaps as important as pointing to good resources, it can help you avoid the many simply terrible examples of bad PHP code that litter the internet!
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

Best of luck with your project, ~Ray
0
 
Chris StanyonCommented:
Without knowing more about the rest of your app, it's difficult to know. For example, what happens in panel/index.php or admin/index.php - it's possible that this page tries to redirect to there, that page doesn't think there's a user logged in so redirects back to the login page - and so on!!

Also, you're logic seems a little off.

If 'type' is anything but Admin, the first if block will be fired.
If it is Admin, then the second block will be fired
The third block will never be fired!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.