Only issue I have is using JavaScript to bounce unauthenticated users since JS can be disabled very easily. Your first script looks all right. I would recommend writing a function to handle that stuff though: test key in POST array, retrieve value from POST array, run mysql_real_escape_string(). Then that part would look something like this:
$username = getVar( 'username' );
$password = md5( getVar( 'password' ) );
...instead of...
if (isset ($_POST['username']) && $_POST['username'] != '')
$username = $_POST['username'];
if(isset ($_POST['password']) && $_POST['password'] != '')
$password = $_POST['password'];
$username = mysql_real_escape_string( $username );
$password = mysql_real_escape_string( $password );
I'll leave the coding of the getVar() function as an exercise for you.
One technique to look into for greater security by default would be parameterizing your queries so the quoting and escaping are handled automatically. PDO is a good solution for this. Good luck.