how to get correct user id on page
For getting user based on current page, you can try to use $account = menu_get_object('user') code.
Var $account will contain user object.
Read more about this function here: https://api.drupal.org/api/drupal/includes%21menu.inc/function/menu_get_object/7