It is in the beginning. But then all of a sudden you have 2 queries that you don't make sure is protected from SQL Injection:
mysql_query("UPDATE users SET loggedin='yes' WHERE username='$user'");
mysql_query("UPDATE users SET ipaddress='$userip' WHERE username='$user'");
You should use mysql_real_escape_string on ALL variables in ALL queries that use variables from the outside.
[Edit]Why do you use 2 queries for this, you only need one. And what happens if the user don't use the logout function but just close the connection? Is he/she still considered to be logged in?[/edit]