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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.