I won’t trust this pagenumber check because when a reference appears at the begin of a pageit can sometimes use the pagenumber of the previous page (hard to notice when you use \pagein your examples to force a page break).