0
votes

I have a database and inside the database i have a user with a user type as admin.

As soon as i created a new user with user type member i was unable to login on my login page.

It gave me an error saying that :

localhost redirected you too many times.

PHP Code:

<?php
 //checks if login session variable exist? If it does, display Logout
 session_start();
 if (isset($_SESSION['login']) && $_SESSION['login'] != "") {
    //link to page logout.php and displays the word Logout + username
    echo "<li><a href='logout.php'>Logout " . $_SESSION['login'] . "</a></li>";
    if (isset($_SESSION['login']) && $_SESSION['usertype'] != "admin") {
        echo ("<li><a href='profile.php'>Profile</a></li>");
    } else {
        echo ("<li><a href='AdminPage.php'>Administrator</a></li>");
    }
    if ($_SESSION['login'] == "") {
        header("Location:login.php");
    }
    if ($_SESSION['login'] != "" && $_SESSION['usertype'] == "member") {
        header("location:home.php");
    }
} else {
    //else link to pagelogin.php and display the word Login
    echo ("<li><a href ='pagelogin.php'>Login</a></li>");
 }
?>

Also have a picture of the login.php codes :

enter image description here

1
Somewhere in the code is redirecting you... we can't help you without any code. - Chin Leung
@ChinLeung i will put in different codes until the error is found - user7496575
What file is this? Is it the login.php, or a header file? - aynber
@aynber this is the nav bar if user type is admin , it will display admin page or else display profile page for members - user7496575
If the nav bar is included in login.php, it will keep redirecting. Either remove the nav bar from login.php, or have a check on if($_SESSION['login'] == "") to make sure that the location already isn't login.php. - aynber

1 Answers

0
votes

First of all if you want to check a variable if its empty you can use the empty(variable).

Then to write your code better:

if(!empty($_SESSION['login']) && $_SESSION['usertype'] =="member")
{
header("location:home.php");
}
else if(empty($_SESSION['login']))
{
    header("location:login.php");
}