I have made a simple login script here. There are 3 files, 1 is functions.php(Containing the login function), then there is userdashboard.php, which contains some user functions and then another file users.php which processes the login.
The problem is, whenever I login, the login in successful but it throws the error :- unknow variable username.
It should display the username of the person logged in, what am I doing wrong ? Here's the code :-
functions.php
<?php
include 'dbconnector.php';
function checklogin($username,$password)
{
    include 'dbconnector.php';
    $userexists=false;
    $username=mysql_real_escape_string($username);
    $password=mysql_real_escape_string($password);
    $password=md5($password);
    $query="select * from f_users where username = '" . $username . "' and password = '" . $password . "'";
    $result=mysql_query($query,$db) or die (mysql_error($db));
    if(mysql_num_rows($result) > 0)
    {
        $userexists=true;
    }
    else
    {
        $userexists=false;
    }
    return $userexists;
}
userdashboard.php
<?php
include('dbconnector.php');
session_start();
if(isset($_SESSION['logged']) && $_SESSION['logged']=1)
{
    $_SESSION['username']=$username;
    echo "Hello " . $username;
}
else
{
    header('Location:login.php');
}
echo '<a href="logout.php">logout</a>';
?>
file where login is processed.
include 'functions.php';
.
.
.
case 'login':
        $username=$_POST['username'];
        $password=$_POST['password'];
        $username=mysql_real_escape_string($username);
        $password=mysql_real_escape_string($password);
        $password=md5($password);
        if((!empty($username)) && (!empty($password)))
        {
            if(!checklogin($username,$password))
            {
                $_SESSION['logged']=1;
                $_SESSION['username']=$username;
                header('Location:userdashboard.php');
            }
            else
            {
                echo "Invalid combination of username and password";
                echo "redirecting to the login page";
                header('refresh:2;URL=login.php');
            }
        }
        else
        {
            echo "username or password fields cannot be empty, redirecting";
            header('refresh:2;URL=login.php');
        }
        break;
Thanks for the fix Houssni. I have a weird error here.
Even if I try a valid combination of username and password, it always goes to the else part and throws the error. What wrong am I doing here ?
$username=mysql_real_escape_string($username);
            $password=mysql_real_escape_string($password);
            $password=md5($password);
            $query="select * from f_users where username = '" . $username . "' and password = '" . $password . "'";
            $result=mysql_query($query,$db) or die (mysql_error($db));
            if(mysql_num_rows($result) > 0)
            {
                session_start();
                $_SESSION['logged']=1;
                $_SESSION['username']=$username;
                header('Location:userdashboard.php');
                exit();
            }
            else
            {
                echo mysql_num_rows($result);
                echo "Invalid combination of username and password";
                echo "redirecting to the login page";
                header('refresh:2;URL=login.php');
                exit();
            }
 
     
    