you can use the isset() function here's a sample
session_start();
if( !isset( $_SESSION['username']) ){
echo 'Unauthorized access';
die();
}
hope this helps...
You see the problem lies in using the left joins. any row even it doesn satisfy the join condition will be fetched that's why u r getting a wrong count.
so either use inner joins or post ur table structures and the result you want to get maybe i can help...