PHP logging in redirection

i'm working on a site and when I try to login, it automatically redirects me back to the login page not allowing me to login. I think its the sessions, but not sure. If someone could please take a look for me. Its a dashboard based site so its supposed to redirect the user to there correct dash`board by there rank, but it seems its not working. Heres the login page:

Login:

    <?php
require 'core/config.php';

if(isset($_POST['submit']))
{
    $username = $db->real_escape_string($_POST['username']);
    $password = md5($_POST['password']);

    if(empty($username) or empty($password))
    {
        echo 'You must fill in both boxes!';
    } else {
        $query = $db->query("SELECT * FROM users WHERE username = '".$username."'");
        while($row = $query->fetch_array())
        {
            $dbpassword = $row['Password'];
        }

        if($password !== $dbpassword)
        {
            echo 'Password was incorrect.';
        } else {
            $query1 = $db->query("SELECT * FROM users WHERE username='".$username."'");
            while($rows = $query1->fetch_array())
            {
                $rank = $rows['Rank'];
            }

            if($rank === 'admin')
            {
                $_SESSION['admin'] = '1';
                $_SESSION['username'] = $username;

                echo '<script>window.location="admin/index.php";</script>';
            }
            elseif ($rank === 'partner')
            {
                registerSession('partner', '1');
                registerSession('username', $username);

                header('Location: partner/');
            }
            elseif ($rank === 'trainee')
            {
                registerSession('trainee', '1');
                registerSession('trainee', '1');

                header('Location: trainee/');
            }
            else 
            {
                echo 'You aren\'t a member.';
            }
        }
    }
}
?>

Code to redirect if not logged in:

<?php
session_start();
if(isset($_SESSION['partner']))
{
$_SESSION['username'];

} else {
header("location: ../index.php")
}
?>

Answers


Seems like the session variable partner is not being set. So

Instead of

registerSession('partner', '1');
registerSession('username', $username);

Make use of

$_SESSION['partner']=1;
$_SESSION['username']=$username;

Try to add session_start(); ob_start(); on the top of your config.php file.


I'm assuming you're logging in as admin or a trainee, not as a partner in which case you don't have a session variable for "partner"

Change your redirect code to something like this:

if(isset($_SESSION['partner'])||isset($_SESSION['admin']||isset($_SESSION['trainee'])))

Need Your Help

Servicestack - Inject class that have constructor

c# dependency-injection servicestack funq

I Have class that have some property injection like this:

Get SoundCloud Artist Page?

javascript node.js soundcloud

I'm trying to write a function in JavaScript where the user can type in an artist, and it will return a link to that artist's SoundCloud page. For example !artist beyonce --> https://soundcloud.com/