(Bug 5265) Check if $u is defined first

Because logged out users can view the account settings page, which means
$u may be undefined here.
Merge pull request #644 from afuna/fix-notloggedin-settings-bug-5265

(Bug 5265) Check if $u is defined first